-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfizzbuzz.js
46 lines (37 loc) · 1.03 KB
/
fizzbuzz.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
export const fizzBuzz1 = (num) => {
return [...Array(num).keys()]
.map((_, i) => i + 1)
.map((i) =>
i % 15 === 0
? 'FizzBuzz'
: i % 3 === 0
? 'Fizz'
: i % 5 === 0
? 'Buzz'
: i
);
};
/*
Break-down:
[...Array(100).keys()]
creates an empty array of length 100, and returns its keys, which are then used to build a new array, so we get [0 ... 99]
.map((_, i) => i + 1)
this replaces each element with its index incremented by 1, so we now have [1 ... 100]
.map(i => ...)
now we apply FizzBuzz logic, by testing all the possible combinations in the correct order, using the ternary operator
*/
export const fizzBuzz2 = (num) => {
let fizzedBuzzedArr = [];
for (let i = 1; i <= num; i++) {
if (i % 3 === 0 && i % 5 === 0) {
fizzedBuzzedArr.push('FizzBuzz');
} else if (i % 3 === 0) {
fizzedBuzzedArr.push('Fizz');
} else if (i % 5 === 0) {
fizzedBuzzedArr.push('Buzz');
} else {
fizzedBuzzedArr.push(i);
}
}
return fizzedBuzzedArr;
};