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:
And to total:
3 + 2*1 + 2*3 + 2 + 1
3 + 2 + 6 + 3
5 + 9
Go through the setup instructions for Javascript to install the necessary dependencies:
https://exercism.io/tracks/javascript/installation
Install assignment dependencies:
$ npm install
Execute the tests with:
$ npm test
In the test suites all tests but the first have been skipped.
Once you get a test passing, you can enable the next one by changing xtest
to
test
.
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.
import { score } from './scrabble-score';
describe('Scrabble', () => {
test('lowercase letter', () => {
expect(score('a')).toEqual(1);
});
xtest('uppercase letter', () => {
expect(score('A')).toEqual(1);
});
xtest('valuable letter', () => {
expect(score('f')).toEqual(4);
});
xtest('short word', () => {
expect(score('at')).toEqual(2);
});
xtest('short, valuable word', () => {
expect(score('zoo')).toEqual(12);
});
xtest('medium word', () => {
expect(score('street')).toEqual(6);
});
xtest('medium, valuable word', () => {
expect(score('quirky')).toEqual(22);
});
xtest('long, mixed-case word', () => {
expect(score('OxyphenButazone')).toEqual(41);
});
xtest('english-like word', () => {
expect(score('pinata')).toEqual(8);
});
xtest('empty input', () => {
expect(score('')).toEqual(0);
});
xtest('entire alphabet available', () => {
expect(score('abcdefghijklmnopqrstuvwxyz')).toEqual(87);
});
});
const LETTERS = {
1: /A|E|I|O|U|L|N|R|S|T/gi,
2: /D|G/gi,
3: /B|C|M|P/gi,
4: /F|H|V|W|Y/gi,
5: /K/gi,
8: /J|X/gi,
10: /Q|Z/gi
};
export const score = word => {
return Object.keys(LETTERS)
.map(key => {
return numberOfMatches(word, LETTERS[key]) * key;
})
.reduce((sum, number) => sum + number);
};
export const numberOfMatches = (word, regex) => {
let matches = word.match(regex);
return matches !== null ? matches.length : 0;
};
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.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments