forked from ejhessing/dom-events-and-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
93 lines (77 loc) · 2.19 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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)
//console.log(test)
//debugger
}
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
bindEventListeners(dots)
for (var i = 0; i < dots.length; i++){
//console.log(i)
if (dots[i].classList.contains('blue')){
totals.blue++;
}
if (dots[i].classList.contains('green')){
totals.green++;
}
if (dots[i].classList.contains('invisible')){
totals.invisible++;
}
}
//debugger
/*
var bluedots = document.getElementsByClassName('blue')
for (var i = 0; i < bluedots.length; i++){
totals.blue++;
}
var greendots = document.getElementsByClassName('green')
for (var i = 0; i < greendots.length; i++){
totals.green++;
}
var invisibledots = document.getElementsByClassName('invisible')
for (var i = 0; i < invisibledots.length; i++){
totals.invisible++;
}
*/
// 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]
}
}