🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of slaymance

slaymance's solution

to Scrabble Score in the JavaScript Track

Published at Oct 21 2020 · 0 comments
Instructions
Test suite
Solution

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:

https://exercism.io/tracks/javascript/installation

Requirements

Please cd into exercise directory before running all below commands.

Install assignment dependencies:

$ npm install

Making the test suite pass

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.

Submitting Solutions

Once you have a solution ready, you can submit it using:

exercism submit scrabble-score.js

Submitting Incomplete Solutions

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

Exercise Source Credits

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

scrabble-score.spec.js

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);
  });
});

scrabble-score.js

//
// This is only a SKELETON file for the 'Scrabble Score' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

/**
 * This is acceptable, right? I think it's acceptable.
 */
import { transform } from '../etl/etl';

const SCORES = transform({
  1: ['A', 'E', 'I', 'O', 'U', 'L', 'N', 'R', 'S', 'T'],
  2: ['D', 'G'],
  3: ['B', 'C', 'M', 'P'],
  4: ['F', 'H', 'V', 'W', 'Y'],
  5: ['K'],
  8: ['J', 'X'],
  10: ['Q', 'Z'],
});

export const score = word => [...word.toLowerCase()].reduce((score, letter) => SCORES[letter] + score, 0);

/**
 * The problem talks about implementing double/triple letters/words but the tests aren't set up in a way to actually
 * know which letters or words should receive bonuses. I'd recommend a data structure that looks like the following:
 * {
 *  word: [
 *    { letter: 'Q', multiplier: 1 },
 *    { letter: 'u', multiplier: 1 },
 *    { letter: 'i', multiplier: 1 },
 *    { letter: 'r', multiplier: 3 },
 *    { letter: 'k', multiplier: 1 },
 *    { letter: 'y', multiplier: 2 },
 *  ],
 *  multiplier: 2
 * }
 * 
 * I'd implement the solution as thus:
 */

export const scoreBonuses = play => play.multiplier * play.word
  .reduce((score, { letter, multiplier }) => multiplier * SCORES[letter.toLowerCase()] + score, 0);

scrabble-score.spec.js

import { score, scoreBonuses } from './scrabble-score';

describe('Scrabble', () => {
  test('lowercase letter', () => {
    expect(score('a')).toEqual(1);
  });

  test('uppercase letter', () => {
    expect(score('A')).toEqual(1);
  });

  test('valuable letter', () => {
    expect(score('f')).toEqual(4);
  });

  test('short word', () => {
    expect(score('at')).toEqual(2);
  });

  test('short, valuable word', () => {
    expect(score('zoo')).toEqual(12);
  });

  test('medium word', () => {
    expect(score('street')).toEqual(6);
  });

  test('medium, valuable word', () => {
    expect(score('quirky')).toEqual(22);
  });

  test('long, mixed-case word', () => {
    expect(score('OxyphenButazone')).toEqual(41);
  });

  test('english-like word', () => {
    expect(score('pinata')).toEqual(8);
  });

  test('empty input', () => {
    expect(score('')).toEqual(0);
  });

  test('entire alphabet available', () => {
    expect(score('abcdefghijklmnopqrstuvwxyz')).toEqual(87);
  });

  test('scoreBonuses', () => {
    expect(scoreBonuses({
      word: [
        { letter: 'Q', multiplier: 1 },
        { letter: 'u', multiplier: 1 },
        { letter: 'i', multiplier: 1 },
        { letter: 'r', multiplier: 3 },
        { letter: 'k', multiplier: 1 },
        { letter: 'y', multiplier: 2 }
      ],
      multiplier: 2
    })).toEqual(56);
  })
});

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?