-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (50 loc) · 1.51 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
54
const btnEl = 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;
// based on random number we'll change the dice face
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 "";
}
}
btnEl.addEventListener("click",()=>{
diceEl.classList.add("roll-animation");
// since animation was only working one time
// set timeout which triggers remove function after 1 second
setTimeout(()=>{
diceEl.classList.remove("roll-animation");
rollDice();
},1000)
})