-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (51 loc) · 1.91 KB
/
app.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
55
56
57
58
59
60
61
62
63
64
var dice, item, rolls, total_rolls;
function randomInt(n) {
// Return a random number from in [0, n[
return Math.floor(Math.random()*n);
}
function randomMember(arr) {
// Return a random member of the array
return arr[randomInt(arr.length)]
}
dice = [1, 2, 3, 4, 5, 6];
rolls = [];
document.getElementById('button_roll').addEventListener('click', (event) => {
rolls.forEach((item) => {
let element_list = document.getElementById('list');
element_list.innerText = randomMember(dice);
});
rolls.unshift(randomMember(dice));
let element_total = document.getElementById('total');
element_total.innerText = rolls.reduce((a,b) => a+b, 0);
let new_ul = document.createElement('ul');
new_ul.innerText = rolls;
new_ul.style.backgroundColor = '#ff0000';
element_total.appendChild(new_ul);
total_rolls = rolls.reduce((a,b) => a+b, 0);
if (total_rolls == 11) {
let element_info = document.getElementById('info');
element_info.innerText = 'You won';
} else if (total_rolls > 11) {
let element_info2 = document.getElementById('info');
element_info2.innerText = 'You lose';
} else {
let element_info3 = document.getElementById('info');
element_info3.innerText = 'Keep playing';
}
});
document.getElementById('button_restart').addEventListener('click', (event) => {
let element_list2 = document.getElementById('list');
rolls = [];
element_list2.replaceChildren();
let element_total2 = document.getElementById('total');
element_total2.innerText = '0';
});
document.getElementById('button_remove').addEventListener('click', (event) => {
let element_total3 = document.getElementById('total');
element_total3.innerText = rolls.reduce((a,b) => a+b, 0);
let new_ul2 = document.createElement('ul');
new_ul2.innerText = rolls;
new_ul2.style.backgroundColor = '#ff0000';
element_total3.appendChild(new_ul2);
});
[][0];