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:
Go through the setup instructions for Javascript to install the necessary dependencies:
https://exercism.io/tracks/javascript/installation
Please cd
into exercise directory before running all below commands.
Install assignment dependencies:
$ npm install
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
.
Once you have a solution ready, you can submit it using:
exercism submit raindrops.js
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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
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}`;
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
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
@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 whichObject.entries
does anyway: