-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizz-buzz.js
58 lines (41 loc) · 1.25 KB
/
fizz-buzz.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
58
{ //block added so both scripts can run on the same web page.
const button = document.getElementById("btn-count");
button.addEventListener("click", function () {
const outputElement = document.getElementById("output");
removeAllChildren(outputElement);
const maxInput = document.getElementById("max");
const max = maxInput.value;
fizzBuzzGame(max);
});
function removeAllChildren(element) {
const children = element.children;
for (let i = 0; i < children.length; i++) {
let current = children[i];
element.removeChild(current);
}
}
function fizzBuzzGame(maxCount) {
const outputElement = document.getElementById("output");
const listTarget = document.createElement("ul");
outputElement.appendChild(listTarget);
let count = 0
while (count < maxCount) {
//Change the counter first so we don't forget
count += 1;
let msg = "";
if (count % 3 === 0) {
msg += "Fizz ";
}
if (count % 5 === 0) {
msg += "Buzz ";
}
if (msg === "") {
msg += count;
}
const item = document.createElement("li");
const text = document.createTextNode(msg);
item.appendChild(text);
listTarget.appendChild(item);
}
}
}