Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of andreasremdt

andreasremdt's solution

to Raindrops in the JavaScript Track

Published at May 16 2021 · 0 comments
Instructions
Test suite
Solution

Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the modulo operation.

The rules of raindrops are that if a given number:

  • has 3 as a factor, add 'Pling' to the result.
  • has 5 as a factor, add 'Plang' to the result.
  • has 7 as a factor, add 'Plong' to the result.
  • does not have any of 3, 5, or 7 as a factor, the result should be the digits of the number.

Examples

  • 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong".
  • 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang".
  • 34 is not factored by 3, 5, or 7, so the result would be "34".

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 raindrops.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

A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. https://en.wikipedia.org/wiki/Fizz_buzz

raindrops.spec.js

import { convert } from './raindrops';

describe('Raindrops', () => {
  test('the sound for 1 is 1', () => expect(convert(1)).toEqual('1'));

  xtest('the sound for 3 is Pling', () => expect(convert(3)).toEqual('Pling'));

  xtest('the sound for 5 is Plang', () => expect(convert(5)).toEqual('Plang'));

  xtest('the sound for 7 is Plong', () => expect(convert(7)).toEqual('Plong'));

  xtest('the sound for 6 is Pling as it has a factor 3', () =>
    expect(convert(6)).toEqual('Pling'));

  xtest('2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base', () =>
    expect(convert(8)).toEqual('8'));

  xtest('the sound for 9 is Pling as it has a factor 3', () =>
    expect(convert(9)).toEqual('Pling'));

  xtest('the sound for 10 is Plang as it has a factor 5', () =>
    expect(convert(10)).toEqual('Plang'));

  xtest('the sound for 14 is Plong as it has a factor of 7', () =>
    expect(convert(14)).toEqual('Plong'));

  xtest('the sound for 15 is PlingPlang as it has factors 3 and 5', () =>
    expect(convert(15)).toEqual('PlingPlang'));

  xtest('the sound for 21 is PlingPlong as it has factors 3 and 7', () =>
    expect(convert(21)).toEqual('PlingPlong'));

  xtest('the sound for 25 is Plang as it has a factor 5', () =>
    expect(convert(25)).toEqual('Plang'));

  xtest('the sound for 27 is Pling as it has a factor 3', () =>
    expect(convert(27)).toEqual('Pling'));

  xtest('the sound for 35 is PlangPlong as it has factors 5 and 7', () =>
    expect(convert(35)).toEqual('PlangPlong'));

  xtest('the sound for 49 is Plong as it has a factor 7', () =>
    expect(convert(49)).toEqual('Plong'));

  xtest('the sound for 52 is 52', () => expect(convert(52)).toEqual('52'));

  xtest('the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7', () =>
    expect(convert(105)).toEqual('PlingPlangPlong'));

  xtest('the sound for 3125 is Plang as it has a factor 5', () =>
    expect(convert(3125)).toEqual('Plang'));
});
export const convert = (num) => {
  const sounds = [];

  if (num % 3 == 0) sounds.push('Pling');
  if (num % 5 == 0) sounds.push('Plang');
  if (num % 7 == 0) sounds.push('Plong');
  if (sounds.length == 0) sounds.push(num);

  return sounds.join('');
};

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?