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

freddie2025's solution

to Raindrops in the JavaScript Track

Published at Apr 06 2020 · 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('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'));
});
//
// This is only a SKELETON file for the 'Raindrops' exercise. It's been provided as a
// convenience to get you started writing code faster.
//

export const convert = (input) => {

  let output = '';

  if(input % 3 == 0) output += 'Pling';		
  if(input % 5 == 0) output += 'Plang';
  if(input % 7 == 0) output += 'Plong';

  return output === '' ? input.toString() : output;
};

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?