-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiceScript.js
More file actions
65 lines (56 loc) · 1.72 KB
/
diceScript.js
File metadata and controls
65 lines (56 loc) · 1.72 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
53
54
55
56
57
58
59
60
61
62
63
64
65
function receiveRoll(){
const diceOptions = document.getElementById("dice");
let selectedDice;
for (let die of dice){
if (die.checked){
selectedDice = die.value;
break;
}
}
if (!selectedDice){
alert("Please select at least one option.");
return;
}
else if (selectedDice > 1){
alert("Please select only one dice at a time.");
return;
}
let diceRoll;
switch (selectedDice) {
case "d4":
diceRoll = Math.floor(Math.random() * 4) + 1;
break;
case "d6":
diceRoll = Math.floor(Math.random() * 6) + 1;
break;
case "d8":
diceRoll = Math.floor(Math.random() * 8) + 1;
break;
case "d10":
diceRoll = Math.floor(Math.random() * 10) + 1;
break;
case "d12":
diceRoll = Math.floor(Math.random() * 12) + 1;
break;
case "d20":
diceRoll = Math.floor(Math.random() * 20) + 1;
break;
case "d100":
diceRoll = Math.floor(Math.random() * 100) + 1;
break;
default:
alert("Please select at least one option.");
return;
}
const resultDiv = document.getElementById("result");
resultDiv.textContent = `You rolled a ${diceRoll}`;
resultDiv.style.display = 'block';
}
function clearPage(){
const questions = document.querySelectorAll('input[type = "checkbox"]')
questions.forEach((question) => {
question.checked = false;
});
const resultDiv = document.getElementById('result');
resultDiv.style.display = 'none';
}