ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Aug 19 2019
·
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.

- 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".

Go through the setup instructions for Javascript to install the necessary dependencies:

https://exercism.io/tracks/javascript/installation

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`

.

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import { convert } from './raindrops';
describe('Raindrops', () => {
test('converts 1', () => expect(convert(1)).toEqual('1'));
xtest('converts 3', () => expect(convert(3)).toEqual('Pling'));
xtest('converts 5', () => expect(convert(5)).toEqual('Plang'));
xtest('converts 7', () => expect(convert(7)).toEqual('Plong'));
xtest('converts 6', () => expect(convert(6)).toEqual('Pling'));
xtest('converts 9', () => expect(convert(9)).toEqual('Pling'));
xtest('converts 10', () => expect(convert(10)).toEqual('Plang'));
xtest('converts 14', () => expect(convert(14)).toEqual('Plong'));
xtest('converts 15', () => expect(convert(15)).toEqual('PlingPlang'));
xtest('converts 21', () => expect(convert(21)).toEqual('PlingPlong'));
xtest('converts 25', () => expect(convert(25)).toEqual('Plang'));
xtest('converts 35', () => expect(convert(35)).toEqual('PlangPlong'));
xtest('converts 49', () => expect(convert(49)).toEqual('Plong'));
xtest('converts 52', () => expect(convert(52)).toEqual('52'));
xtest('converts 105', () => expect(convert(105)).toEqual('PlingPlangPlong'));
xtest('converts 12121', () => expect(convert(12121)).toEqual('12121'));
});
```

```
const SOUNDS = {
3: "Pling",
5: "Plang",
7: "Plong"
};
export const convert = number => {
let sound = Object.keys(SOUNDS)
.map(key => {
if (number % key == 0) {
return SOUNDS[key];
}
})
.join("");
if (sound.length > 0) {
return sound;
} else {
return number.toString();
}
};
```

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?

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**.

## Community comments