Avatar of brunnock

brunnock's solution

to High Scores in the JavaScript Track

Published at Nov 20 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

Manage a game player's High Score list.

Your task is to build a high-score component of the classic Frogger game, one of the highest selling and addictive games of all time, and a classic of the arcade era. Your task is to write methods that return the highest score from the list, the last added score, the three highest scores, and a report on the difference between the last and the highest scores.

Setup

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

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

Requirements

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 Incomplete Solutions

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

high-scores.spec.js

import { HighScores } from './high-scores';

describe('High Scores Test Suite', () => {
  test('List of scores', () => {
    const input = [30, 50, 20, 70];
    expect(new HighScores(input).scores).toEqual([30, 50, 20, 70]);
  });

  xtest('Latest score', () => {
    const input = [100, 0, 90, 30];
    expect(new HighScores(input).latest).toEqual(30);
  });

  xtest('Highest score', () => {
    const input = [40, 100, 70];
    expect(new HighScores(input).highest).toEqual(100);
  });

  xtest('Personal bests', () => {
    const input = [50, 30, 10];
    expect(new HighScores(input).top).toEqual([50, 30, 10]);
  });

  xtest('Personal bests highest to lowest', () => {
    const input = [20, 10, 30];
    expect(new HighScores(input).top).toEqual([30, 20, 10]);
  });

  xtest('Personal bests when there is a tie', () => {
    const input = [40, 20, 40, 30];
    expect(new HighScores(input).top).toEqual([40, 40, 30]);
  });

  xtest('Personal bests when there are less than 3', () => {
    const input = [30, 70];
    expect(new HighScores(input).top).toEqual([70, 30]);
  });

  xtest('Personal bests when there is only one', () => {
    const input = [40];
    expect(new HighScores(input).top).toEqual([40]);
  });

  xtest('Personal bests from a long list', () => {
    const input = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70];
    expect(new HighScores(input).top).toEqual([100, 90, 70]);
  });

  xtest('Message for new personal best', () => {
    const input = [20, 40, 0, 30, 70];
    expect(new HighScores(input).report).toEqual("Your latest score was 70. That's your personal best!");
  });

  xtest('Message when latest score is not the highest score', () => {
    const input = [20, 100, 0, 30, 70];
    expect(new HighScores(input).report).toEqual("Your latest score was 70. That's 30 short of your personal best!");
  });

  xtest('Message for repeated personal best', () => {
    const input = [20, 70, 50, 70, 30];
    expect(new HighScores(input).report).toEqual("Your latest score was 30. That's 40 short of your personal best!");
  });
});
class HighScores  {
  constructor(scores) {
    this.scores = scores;
    this.latest = scores[scores.length-1];
    this.highest = Math.max(...scores);
    this.top = [...scores].sort((x,y)=>y-x).slice(0,3);
    this.report = 'Your latest score was ' +this.latest;
    this.report += (this.latest === this.highest) ?
      ". That's your personal best!" :
      (". That's " +(this.highest-this.latest) +' short of your personal best!');
  }
};

module.exports = {HighScores};

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?