Avatar of PatrickMcSweeny

PatrickMcSweeny's solution

to Raindrops in the JavaScript Track

Published at Aug 19 2019 · 0 comments
Instructions
Test suite
Solution

Convert a number to a string, the contents of which depend on the number's factors.

  • If the number has 3 as a factor, output 'Pling'.
  • If the number has 5 as a factor, output 'Plang'.
  • If the number has 7 as a factor, output 'Plong'.
  • If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

Examples

  • 28's factors are 1, 2, 4, 7, 14, 28.
    • In raindrop-speak, this would be a simple "Plong".
  • 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
    • In raindrop-speak, this would be a "PlingPlang".
  • 34 has four factors: 1, 2, 17, and 34.
    • In raindrop-speak, this would be "34".

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.

Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

Submitting Incomplete Solutions

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

raindrops.spec.js

import { convert } from './raindrops';

describe('Raindrops', () => {
  test('converts 1', () => expect(convert(1)).toEqual('1'));

  xtest('converts 3', () => expect(convert(3)).toEqual('Pling'));

  xtest('converts 5', () => expect(convert(5)).toEqual('Plang'));

  xtest('converts 7', () => expect(convert(7)).toEqual('Plong'));

  xtest('converts 6', () => expect(convert(6)).toEqual('Pling'));

  xtest('converts 9', () => expect(convert(9)).toEqual('Pling'));

  xtest('converts 10', () => expect(convert(10)).toEqual('Plang'));

  xtest('converts 14', () => expect(convert(14)).toEqual('Plong'));

  xtest('converts 15', () => expect(convert(15)).toEqual('PlingPlang'));

  xtest('converts 21', () => expect(convert(21)).toEqual('PlingPlong'));

  xtest('converts 25', () => expect(convert(25)).toEqual('Plang'));

  xtest('converts 35', () => expect(convert(35)).toEqual('PlangPlong'));

  xtest('converts 49', () => expect(convert(49)).toEqual('Plong'));

  xtest('converts 52', () => expect(convert(52)).toEqual('52'));

  xtest('converts 105', () => expect(convert(105)).toEqual('PlingPlangPlong'));

  xtest('converts 12121', () => expect(convert(12121)).toEqual('12121'));
});
const SOUNDS = {
  3: "Pling",
  5: "Plang",
  7: "Plong"
};

export const convert = number => {
  let sound = Object.keys(SOUNDS)
    .map(key => {
      if (number % key == 0) {
        return SOUNDS[key];
      }
    })
    .join("");

  if (sound.length > 0) {
    return sound;
  } else {
    return number.toString();
  }
};

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?