 # rootulp's solution

## to Scrabble Score in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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

## Letter Values

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
``````

## Examples

"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

## Extensions

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

## Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

## Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

``````npm install -g jasmine
``````

Run the test suite from the exercise directory with:

``````jasmine scrabble-score.spec.js
``````

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing `xit` to `it`.

## Source

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

## Submitting Incomplete Solutions

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

### scrabble-score.spec.js

``````var score = require('./scrabble-score');

describe('Scrabble', function () {
it('scores an empty word as zero', function () {
expect(score('')).toEqual(0);
});

xit('scores a null as zero', function () {
expect(score(null)).toEqual(0);
});

xit('scores a very short word', function () {
expect(score('a')).toEqual(1);
});

xit('scores the word by the number of letters', function () {
expect(score('street')).toEqual(6);
});

xit('scores more complicated words with more', function () {
expect(score('quirky')).toEqual(22);
});

xit('scores case insensitive words', function () {
expect(score('OXYPHENBUTAZONE')).toEqual(41);
});
});``````
``````function score(word) {
'use strict';

var points = {
'A' : 1, 'E' : 1, 'I' : 1, 'O' : 1, 'U' : 1, 'L' : 1, 'N' : 1,
'R' : 1, 'S' : 1, 'T' : 1, 'D' : 2, 'G' : 2, 'B' : 3, 'C' : 3,
'M' : 3, 'P' : 3, 'F' : 4, 'H' : 4, 'V' : 4, 'W' : 4, 'Y' : 4,
'K' : 5, 'J' : 8, 'X' : 8, 'Q' : 10, 'Z' : 10
}

var calcScore = function(word) {
word = word || ""
word = word.toUpperCase();

var total = 0;
for(var i = 0; i < word.length; i++) {
total += points[word[i]]
}
}

return calcScore(word);
}

module.exports = score``````