-
Notifications
You must be signed in to change notification settings - Fork 428
/
game.js
66 lines (56 loc) · 1.7 KB
/
game.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
65
66
// Don't change or delete this line! It waits until the DOM has loaded, then calls
// the start function. More info:
// https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
document.addEventListener('DOMContentLoaded', start)
function start () {
bindEventListeners(document.getElementsByClassName('board')[0].children)
}
function bindEventListeners (dots) {
for (var i = 0; i < dots.length; i++) {
// BIND YOUR EVENT LISTENERS HERE
// The first one is provided for you
dots[i].addEventListener('contextmenu', makeGreen)
dots[i].addEventListener('click', makeBlue)
dots[i].addEventListener('dblclick', hide)
}
}
function makeGreen (evt) {
evt.preventDefault()
evt.target.classList.toggle('green')
updateCounts()
}
// CREATE FUNCTION makeBlue HERE
function makeBlue (evt) {
evt.target.classList.toggle('blue')
updateCounts()
}
// CREATE FUNCTION hide HERE
function hide (evt) {
evt.target.classList.toggle('invisible')
updateCounts()
}
function updateCounts () {
var totals = {
blue: 0,
green: 0,
invisible: 0
}
// WRITE CODE HERE TO COUNT BLUE, GREEN, AND INVISIBLE DOTS
var dots = document.getElementsByClassName('board')[0].children
for (var i = 0; i < dots.length; i++ ) {
if (dots[i].classList.contains('invisible')) {
totals.invisible++
} else if (dots[i].classList.contains('green')) {
totals.green++
} else if (dots[i].classList.contains('blue')) {
totals.blue++
}
}
// Once you've done the counting, this function will update the display
displayTotals(totals)
}
function displayTotals (totals) {
for (var key in totals) {
document.getElementById(key + '-total').innerHTML = totals[key]
}
}