-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (54 loc) · 2.27 KB
/
script.js
File metadata and controls
62 lines (54 loc) · 2.27 KB
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
// Event listeners for the Generate and Clear buttons
document.getElementById('generate').addEventListener('click', generateDivs);
document.getElementById('clear').addEventListener('click', clearDivs);
// Function to generate the FizzBuzz divs
function generateDivs() {
clearDivs(); // Clear any existing divs
const main = document.getElementById('main');
const startValue = parseInt(document.getElementById('startValue').value);
const endValue = parseInt(document.getElementById('endValue').value);
const fizzValue = parseInt(document.getElementById('fizzValue').value);
const buzzValue = parseInt(document.getElementById('buzzValue').value);
// Validation checks for user input
if (isNaN(startValue) || isNaN(endValue) || isNaN(fizzValue) || isNaN(buzzValue)) {
alert('Please enter valid numbers for all fields.');
return;
}
if (startValue > endValue) {
alert('Start value should be less than or equal to end value.');
return;
}
// Loop through the range and create divs based on FizzBuzz logic
for (let i = startValue; i <= endValue; i++) {
const div = document.createElement('div');
div.id = `myid${i}`;
// Assign appropriate class and text based on the FizzBuzz rules
if (i % fizzValue === 0 && i % buzzValue === 0) {
div.className = 'fizzbuzz';
div.innerText = 'FizzBuzz';
} else if (i % fizzValue === 0) {
div.className = 'fizz';
div.innerText = 'Fizz';
} else if (i % buzzValue === 0) {
div.className = 'buzz';
div.innerText = 'Buzz';
} else {
div.className = 'number';
div.innerText = i;
}
// Append the div to the main container
main.appendChild(div);
// Animate the div for a nicer visual effect
setTimeout(() => {
div.style.opacity = 1;
div.style.transform = 'translateY(0)';
}, 50 * (i - startValue)); // Stagger the animation
}
}
// Function to clear all generated divs
function clearDivs() {
const main = document.getElementById('main');
while (main.firstChild) {
main.removeChild(main.firstChild);
}
}