-
Notifications
You must be signed in to change notification settings - Fork 0
/
fizzbuzz_webpage.js
57 lines (49 loc) · 1.6 KB
/
fizzbuzz_webpage.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
/**
* ==============================
* 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) {
// Get the sequence container
const sequenceContainer = document.getElementById("sequence");
sequenceContainer.innerHTML = ''; // Clear previous sequences
for (let i = 1; i <= n; i++) {
let sequence = '';
for (const [divisor, word] of Object.entries(fizzbuzzMap)) {
if (i % divisor === 0) {
sequence += word;
}
}
// Append the sequence to the sequenceContainer
const output = document.createElement('div');
output.textContent = sequence || i;
sequenceContainer.appendChild(output);
}
}
/**
* Main function - FizzBuzz program
*/
function main() {
document.getElementById('submit').addEventListener('click', () => {
// Input N
const n = Number(document.getElementById('upper_bound').value);
// Input validation
if (!Number.isInteger(n) || n <= 0) {
alert("Invalid input. Please enter a positive integer.");
}
// FizzBuzz ruleset
const fizzbuzzMap = {3: 'Fizz', 5: 'Buzz'};
// Sequence generation
fizzbuzzGenerator(n, fizzbuzzMap);
});
}
// Call the main function
main();