-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (46 loc) · 1.16 KB
/
script.js
File metadata and controls
52 lines (46 loc) · 1.16 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
const rollDice = document.getElementById("rollDice");
const dice = document.getElementById("dice");
const result = document.getElementById("result");
let historyList = [];
function rollTheDice() {
const rollResult = Math.floor(Math.random() * 6) + 1;
const diceFace = getDiceFace(rollResult);
dice.innerHTML = diceFace;
historyList.push(rollResult);
updateRollHistory();
}
function updateRollHistory() {
result.innerHTML = "";
for (let i = 0; i < historyList.length; i++) {
const listItem = document.createElement("li");
listItem.innerHTML = `Roll ${i + 1}: <span>${getDiceFace(
historyList[i]
)}</span>`;
result.appendChild(listItem);
}
}
function getDiceFace(rollResult) {
switch (rollResult) {
case 1:
return "⚀";
case 2:
return "⚁";
case 3:
return "⚂";
case 4:
return "⚃";
case 5:
return "⚄";
case 6:
return "⚅";
default:
return "";
}
}
rollDice.addEventListener("click", () => {
dice.classList.add("roll-animation")
setTimeout(() => {
dice.classList.remove("roll-animation");
rollTheDice();
}, 1000)
});