Skip to content

Commit 6d61e9c

Browse files
update roll dice simulator
1 parent 0643a11 commit 6d61e9c

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

Diff for: projects/dice-roll-simulator/index.html

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
<!DOCTYPE html>
2-
<html>
2+
<html lang="en">
33
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
47
<title>Dice Roll Simulator</title>
58
<link rel="stylesheet" href="style.css" />
69
</head>
710
<body>
811
<h1>Dice Roll Simulator</h1>
9-
<div id="dice" class="dice"></div>
12+
<div class="dice" id="dice">&#9860;</div>
1013
<button id="roll-button">Roll Dice</button>
1114
<ul id="roll-history">
12-
<li>Roll 1: <span>&#9856;</span></li>
13-
<li>Roll 2: <span>&#9856;</span></li>
15+
<!-- <li>Roll 1: <span>&#9856;</span></li>
16+
<li>Roll 2: <span>&#9860;</span></li> -->
1417
</ul>
15-
16-
<script src="dice-roll.js"></script>
18+
<script src="index.js"></script>
1719
</body>
1820
</html>
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
1-
const diceElement = document.getElementById("dice");
2-
const rollButton = document.getElementById("roll-button");
3-
const rollHistory = document.getElementById("roll-history");
1+
const buttonEl = document.getElementById("roll-button");
2+
3+
const diceEl = document.getElementById("dice");
4+
5+
const rollHistoryEl = document.getElementById("roll-history");
6+
47
let historyList = [];
58

69
function rollDice() {
710
const rollResult = Math.floor(Math.random() * 6) + 1;
811
const diceFace = getDiceFace(rollResult);
9-
diceElement.textContent = diceFace;
12+
diceEl.innerHTML = diceFace;
1013
historyList.push(rollResult);
1114
updateRollHistory();
1215
}
1316

17+
function updateRollHistory() {
18+
rollHistoryEl.innerHTML = "";
19+
for (let i = 0; i < historyList.length; i++) {
20+
const listItem = document.createElement("li");
21+
listItem.innerHTML = `Roll ${i + 1}: <span>${getDiceFace(
22+
historyList[i]
23+
)}</span>`;
24+
rollHistoryEl.appendChild(listItem);
25+
}
26+
}
27+
1428
function getDiceFace(rollResult) {
1529
switch (rollResult) {
1630
case 1:
17-
return "";
31+
return "&#9856;";
1832
case 2:
19-
return "";
33+
return "&#9857;";
2034
case 3:
21-
return "";
35+
return "&#9858;";
2236
case 4:
23-
return "";
37+
return "&#9859;";
2438
case 5:
25-
return "";
39+
return "&#9860;";
2640
case 6:
27-
return "";
41+
return "&#9861;";
2842
default:
2943
return "";
3044
}
3145
}
3246

33-
function updateRollHistory() {
34-
rollHistory.innerHTML = "";
35-
for (let i = 0; i < historyList.length; i++) {
36-
const listItem = document.createElement("li");
37-
listItem.innerHTML = `Roll ${i + 1}: <span>${getDiceFace(
38-
historyList[i]
39-
)}</span>`;
40-
rollHistory.appendChild(listItem);
41-
}
42-
}
43-
44-
rollButton.addEventListener("click", () => {
45-
diceElement.classList.add("roll-animation");
47+
buttonEl.addEventListener("click", () => {
48+
diceEl.classList.add("roll-animation");
4649
setTimeout(() => {
47-
diceElement.classList.remove("roll-animation");
50+
diceEl.classList.remove("roll-animation");
4851
rollDice();
4952
}, 1000);
5053
});

Diff for: projects/dice-roll-simulator/style.css

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ h1 {
2424
0% {
2525
transform: rotateY(0deg) rotateX(0deg);
2626
}
27+
2728
100% {
2829
transform: rotateY(720deg) rotateX(720deg);
2930
}
@@ -33,9 +34,8 @@ button {
3334
background-color: #47a5c4;
3435
color: white;
3536
font-size: 1.5rem;
36-
border: none;
3737
padding: 1rem 2rem;
38-
margin: 5px;
38+
border: none;
3939
border-radius: 1rem;
4040
cursor: pointer;
4141
transition: background-color 0.3s ease;
@@ -48,9 +48,8 @@ button:hover {
4848
ul {
4949
list-style: none;
5050
padding: 0;
51-
margin: 2rem;
5251
max-width: 600px;
53-
margin: 0 auto;
52+
margin: 2rem auto;
5453
}
5554

5655
li {

0 commit comments

Comments
 (0)