-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (25 loc) · 837 Bytes
/
script.js
File metadata and controls
28 lines (25 loc) · 837 Bytes
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
const rollBtn = document.getElementById("roll-btn")
const dicesCountEl = document.getElementById("dices-count");
const sideSelectionEl = document.getElementById("sides");
const rollsContainerEl = document.getElementById("rolls-container");
rollBtn.addEventListener("click", roll)
function roll() {
resetRollsContainer();
const diceCount = dicesCountEl.value;
for (let idx=0; idx<diceCount; idx++) {
addRolledDice();
}
}
function resetRollsContainer() {
rollsContainerEl.innerHTML = "";
}
function addRolledDice() {
const rolledDiceEl = document.createElement("div");
rolledDiceEl.className = "roll";
rolledDiceEl.innerHTML = getRandomRollValue();
rollsContainerEl.appendChild(rolledDiceEl);
}
function getRandomRollValue() {
const sides = sideSelectionEl.value
return Math.floor(Math.random()* sides) + 1;
}