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

michal9909's solution

to Raindrops in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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 ECMAScript to install the necessary dependencies:

http://exercism.io/languages/ecmascript

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 Raindrops from './raindrops';

describe('Raindrops', () => {
  const drops = new Raindrops();

  test('converts 1', () => expect(drops.convert(1)).toEqual('1'));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  xtest('converts 12121', () => expect(drops.convert(12121)).toEqual('12121'));
});
export default class Raindrops {
  constructor() { }

  convert(v) {
    let r = ''

    if (!(v % 3)) {
      r += 'Pling';
    }

    if (v % 5 === 0) {
      r += 'Plang';
    }

    if (Number.isInteger(v / 7)) {
      r += 'Plong'
    }

    return r || `${v}`;
  }
}

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?