-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
53 lines (46 loc) · 1.18 KB
/
script.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
const buttonEl = document.getElementById("roll-button");
const diceEl = document.getElementById("dice");
const rollHistoryEl = document.getElementById("roll-history");
let historyList = [];
function rollDice() {
const rollResult = Math.floor(Math.random() * 6) + 1;
const diceFace = getDiceFace(rollResult);
diceEl.innerHTML = diceFace;
historyList.push(rollResult);
updateRollHistory();
}
function updateRollHistory() {
rollHistoryEl.innerHTML = "";
for (let i = 0; i < historyList.length; i++) {
const listItem = document.createElement("li");
listItem.innerHTML = `Roll ${i + 1}: <span>${getDiceFace(
historyList[i]
)}</span>`;
rollHistoryEl.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 "";
}
}
buttonEl.addEventListener("click", () => {
diceEl.classList.add("roll-animation");
setTimeout(() => {
diceEl.classList.remove("roll-animation");
rollDice();
}, 1000);
});