-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz_console.js
50 lines (43 loc) · 1.22 KB
/
fizzbuzz_console.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
/**
* ==============================
* FizzBuzz - Fulll hiring test
* ==============================
* > Thomas Rigole
* ------------------------------
*/
/**
* Generates sequences of values/words based on the fizzbuzzMap ruleset.
*
* @param {number} n - Upper bound of the FizzBuzz algorithm
* @param {Object} fizzbuzzMap - Ruleset composed of divisor(s) and associated word(s)
*/
function fizzbuzzGenerator(n, fizzbuzzMap) {
for (let i = 1; i <= n; i++) {
let sequence = '';
for (const [divisor, word] of Object.entries(fizzbuzzMap)) {
if (i % divisor === 0) {
sequence += word;
}
}
// Console prompt
console.log(sequence || i);
}
}
/**
* Main function - FizzBuzz program
*/
function main() {
// Input N
let n = Number(prompt("Enter the upper bound N: "));
// Input validation
if (!Number.isInteger(n) || n <= 0) {
console.error("Invalid input. Please enter a positive integer.");
return; // Exit if error (invalid format)
}
// FizzBuzz ruleset
const fizzbuzzMap = {3: 'Fizz', 5: 'Buzz'};
// Sequence generation
fizzbuzzGenerator(n, fizzbuzzMap);
}
// Call the main function
main();