Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

Published at Jul 13 2018
·
1 comment

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:

http://exercism.io/languages/javascript/installation

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

```
npm install -g jasmine
```

Run the test suite from the exercise directory with:

```
jasmine raindrops.spec.js
```

In many test suites all but the first test have been marked "pending".
Once you get a test passing, activate the next one by changing `xit`

to `it`

.

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.

```
var Raindrops = require('./raindrops');
describe('Raindrops', function () {
var drops = new Raindrops();
it('converts 1', function () {
expect(drops.convert(1)).toEqual('1');
});
xit('converts 3', function () {
expect(drops.convert(3)).toEqual('Pling');
});
xit('converts 5', function () {
expect(drops.convert(5)).toEqual('Plang');
});
xit('converts 7', function () {
expect(drops.convert(7)).toEqual('Plong');
});
xit('converts 6', function () {
expect(drops.convert(6)).toEqual('Pling');
});
xit('converts 9', function () {
expect(drops.convert(9)).toEqual('Pling');
});
xit('converts 10', function () {
expect(drops.convert(10)).toEqual('Plang');
});
xit('converts 14', function () {
expect(drops.convert(14)).toEqual('Plong');
});
xit('converts 15', function () {
expect(drops.convert(15)).toEqual('PlingPlang');
});
xit('converts 21', function () {
expect(drops.convert(21)).toEqual('PlingPlong');
});
xit('converts 25', function () {
expect(drops.convert(25)).toEqual('Plang');
});
xit('converts 35', function () {
expect(drops.convert(35)).toEqual('PlangPlong');
});
xit('converts 49', function () {
expect(drops.convert(49)).toEqual('Plong');
});
xit('converts 52', function () {
expect(drops.convert(52)).toEqual('52');
});
xit('converts 105', function () {
expect(drops.convert(105)).toEqual('PlingPlangPlong');
});
xit('converts 12121', function () {
expect(drops.convert(12121)).toEqual('12121');
});
});
```

```
'use strict'
var Left = function(x) {
this.__value = x;
};
Left.of = function(x) {
return new Left(x);
}
Left.prototype.map = function(f) {
return this; // do not map
}
var Right = function(x) {
this.__value = x;
};
Right.of = function(x) {
return new Right(x);
}
Right.prototype.map = function(f) {
return new Right.of(f(this.__value));
}
var map = (f) => (x) => x.map(f);
var id = (x) => x;
var compose = (...fs) => (init) =>
fs.reduceRight((val, f) => f(val), init);
// factor :: num => num => boolean
var factor = (factor) => (num) => num % factor === 0;
var either = (f, g) => (e) => {
switch (e.constructor) {
case Left:
return f(e.__value);
case Right:
return g(e.__value);
}
}
var string = (num) => num.toString();
var log = (msg) => (x) => {
console.log(msg, x);
return x;
}
// getFactors :: number => either(number, string)
var getFactors = (num) => {
var res
= (factor(3)(num) ? 'Pling' : '')
+ (factor(5)(num) ? 'Plang' : '')
+ (factor(7)(num) ? 'Plong' : '')
;
// no factors is treated as an error
return (res.length === 0)
? Left.of(num)
: Right.of(res);
};
class Raindrops {
convert(num) {
return compose(
either(string, id), getFactors
)(num);
}
}
module.exports = Raindrops;
```

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

first time trying to implement some functional error handling via Left, Right, and either.