Published at Jul 13 2018
·
0 comments

Instructions

Test suite

Solution

Given a word, compute the scrabble score for that word.

You'll need these:

```
Letter Value
A, E, I, O, U, L, N, R, S, T 1
D, G 2
B, C, M, P 3
F, H, V, W, Y 4
K 5
J, X 8
Q, Z 10
```

"cabbage" should be scored as worth 14 points:

- 3 points for C
- 1 point for A, twice
- 3 points for B, twice
- 2 points for G
- 1 point for E

And to total:

`3 + 2*1 + 2*3 + 2 + 1`

- =
`3 + 2 + 6 + 3`

- =
`5 + 9`

- = 14

- You can play a double or a triple letter.
- You can play a double or a triple word.

To run the tests, run the command `busted`

from within the exercise directory.

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
local score = require('scrabble-score').score
describe('scrabble-score', function()
it('scores an empty word as zero', function()
assert.are.equal(0, score(''))
end)
it('scores a null as zero', function()
assert.are.equal(0, score(null))
end)
it('scores a very short word', function()
assert.are.equal(1, score('a'))
end)
it('scores the word by the number of letters', function()
assert.are.equal(6, score('street'))
end)
it('scores more complicated words with more', function()
assert.are.equal(22, score('quirky'))
end)
it('scores case insensitive words', function()
assert.are.equal(41, score('OXYPHENBUTAZONE'))
end)
end)
```

```
local scrabble_score = { a=1, b=3, c=3, d=2, e=1, f=4, g=2, h=4, i=1, j=8, k=5, l=1, m=3, n=1, o=1, p=3, q=10, r=1, s=1, t=1, u=1, v=4, w=4, x=8, y=4, z=10}
local function score(input)
local s = input or ''
local sum = 0
for i = 1, #s do
sum = sum + scrabble_score[s:sub(i,i):lower()]
end
return sum
end
return {
score = score
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,276 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments