-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdice.js
More file actions
63 lines (50 loc) · 1.47 KB
/
dice.js
File metadata and controls
63 lines (50 loc) · 1.47 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
class Dice {
constructor(name, numbers, color) {
this.name = name
this.numbers = numbers
this.color = color
this.number
this.div
}
roll() {
this.number = this.numbers[Math.floor(Math.random()*this.numbers.length)];
return this.number
}
createDiv() {
let div = createDiv('ingredient')
let leftDiv = createDiv('left')
let nameDiv = createDiv('name')
let diceDiv = createDiv('dice')
let rollDiv = createDiv('roll')
div.style.background = this.color
nameDiv.innerText = this.name
// numbers
let t = '[ '
for (let n of this.numbers) {
t += n + ', '
}
t = t.slice(0, -2)
t += ' ]'
diceDiv.innerText = t
rollDiv.innerText = this.number
leftDiv.appendChild(nameDiv)
leftDiv.appendChild(diceDiv)
div.appendChild(leftDiv)
div.appendChild(rollDiv)
div.onclick = this.onclick
return div
}
onclick() {
if (this.parentElement.id == 'ingredients') {
let name = this.children[0].children[0].innerText
addIngredient(name)
} else if (this.parentElement.id == 'shaker') {
// let name = this.children[0].children[0].innerText
removeIngredient(this)
}
}
updateDiv() {
if (!this.div) { return }
this.div.children[1].innerText = this.number
}
}