-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustom-FizzBuzz-Array.js
36 lines (29 loc) · 1.52 KB
/
Custom-FizzBuzz-Array.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
/*
Write a function that returns a (custom) FizzBuzz sequence of the numbers 1 to 100.
The function should be able to take up to 4 arguments:
The 1st and 2nd arguments are strings, "Fizz" and "Buzz" by default;
The 3rd and 4th arguments are integers, 3 and 5 by default.
Thus, when the function is called without arguments, it will return the classic FizzBuzz sequence up to 100:
[ 1, 2, "Fizz", 4, "Buzz", "Fizz", 7, ... 14, "FizzBuzz", 16, 17, ... 98, "Fizz", "Buzz" ]
When the function is called with (up to 4) arguments, it should return a custom FizzBuzz sequence, for example:
('Hey', 'There') --> [ 1, 2, "Hey", 4, "There", "Hey", ... ]
('Foo', 'Bar', 2, 3) --> [ 1, "Foo", "Bar", "Foo", 5, "FooBar", 7, ... ]
Examples
fizzBuzzCustom()[15] // returns 16
fizzBuzzCustom()[44] // returns "FizzBuzz" (45 is divisible by 3 and 5)
fizzBuzzCustom('Hey', 'There')[25] // returns 26
fizzBuzzCustom('Hey', 'There')[11] // returns "Hey" (12 is divisible by 3)
fizzBuzzCustom("What's ", "up?", 3, 7)[80] // returns "What's " (81 is divisible by 3)
*/
// Answer:
var fizzBuzzCustom = function(stringOne = "Fizz", stringTwo = "Buzz", numOne = 3, numTwo = 5) {
let result = [];
for (let i = 1; i <= 100; i++) {
if (i % (numOne * numTwo) === 0) result.push(stringOne + stringTwo)
else if (i % numOne === 0) result.push(stringOne)
else if (i % numTwo === 0) result.push(stringTwo)
else result.push(i)
}
return result;
};
// BigO: O(1)