🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # slaymance's solution

## to Raindrops in the JavaScript Track

Published at Jun 05 2021 · 2 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'));
});``````
``````/**
* Check out all my solutions to the Exercism JavaScript track:
* github.com/slaymance/exercism/tree/main/javascript
*/

const RAIN_MAPPING = {
3: 'Pling',
5: 'Plang',
7: 'Plong'
};

/**
* You may notice that I'm able to perform modulo using the factor from RAIN_MAPPING directly, even though JavaScript
* object keys are always strings. This is because % (along with / *) is a multiplicative operator. JavaScript converts
* both expressions to the left and right of a multiplicative operator to a number before evaluating the expression.
* For example:
* 3 % '3' === 0,
* 3 * '3' === 9,
* 3 / '3' === 1, but
* 3 + '3' === '33'
* Since + can be used in string concatenation, the number coercion doesn't take place. You can also do subtraction
* with a string and number:
* 3 - '3' === 0
*/
export const convert = num => Object.entries(RAIN_MAPPING)
.reduce((acc, [factor, rainDrop]) => num % factor ? acc : acc + rainDrop, '') || `\${num}`;``````

## Community comments

Find this solution interesting? Ask the author a question to learn more. Interesting solution! My solution is fairly basic with 3 conditionals and some string concatenation. Do you reckon there's any performance benefits to doing it this way? I guess it's more scalable as changes only need to be made on the mapping Solution Author
commented 1 day ago

@DavidRyan If anything, I would say there's a performance downside by using `Object.entries` since it creates a new array. You could bypass this by creating nested arrays which `Object.entries` does anyway:

``````const RAIN_MAPPING = [
[3, 'Pling'],
[5, 'Plang'],
[7, 'Plong'],
];

export const convert = num => RAIN_MAPPING
.reduce((acc, [factor, rainDrop]) => num % factor ? acc : acc + rainDrop, '') || `\${num}`;
``````
(edited 1 day ago)

### 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?