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'));
});
export const convert = (num) => {
let result = '';
if (!(num % 3 === 0 || num % 5 === 0 || num % 7 === 0)) {
return result += num;
}
if (num % 3 === 0) {
result += "Pling";
}
if (num % 5 === 0) {
result += "Plang";
}
if (num % 7 === 0) {
result += "Plong"
}
return result;
};
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