-
Notifications
You must be signed in to change notification settings - Fork 0
/
raindrops.js
63 lines (56 loc) · 1.44 KB
/
raindrops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const raindropSpeak = (input) => {
// Verify inputs to be a non-zero integer number.
if (typeof (input) !== 'number' || !Number.isInteger(input) || input === 0) {
return undefined;
}
const primeFactors = primeFactorsOf(input);
let result = '';
if (primeFactors.indexOf(3) !== -1) {
// If there is a 3 in the facotrs.
result += 'Pling'
}
if (primeFactors.indexOf(5) !== -1) {
// If there is a 5 in the facotrs.
result += 'Plang'
}
if (primeFactors.indexOf(7) !== -1) {
// If there is a 7 in the facotrs.
result += 'Plong'
}
if (result == '') {
// If there the result does not contain 3,5 or 7
return input;
}
return result;
}
const primeFactorsOf = (number) => {
let negativity;
if (number < 0) {
negativity = true;
number = number * -1;
}
// Get the prime factors of a number.
let factors = [], test = 2, result;
while (number !== 1) {
if (number % test === 0) {
factors.push(test);
number /= test;
} else {
test++;
}
}
// Remove duplicates
result = Array.from(new Set(factors));
// If the number was negative
if (negativity) {
result.push(-1);
}
return result;
}
console.log('==============')
console.log('I am about to run this program on an input of 28.')
console.log('...Starting')
console.log('The resullt is: ', raindropSpeak(28));
console.log('...Goodbye')
console.log('==============')
module.exports = raindropSpeak;