-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (100 loc) · 3.37 KB
/
index.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
let boxes = document.querySelectorAll('.box');
const sizes = document.querySelectorAll('.size');
const gridSize = document.querySelectorAll('.grid');
const grid = document.querySelector('.grid');
const homeButton = document.getElementById('returnHome');
let count = 0;
let x = 3;
let y = 3;
const counterElement = document.getElementById('counter');
counterElement.classList.toggle('hidden');
function randomize(){
alert("When you click on a box it will turn the opposite color along with its adjacent boxes. You have to turn all the boxes the same color- yellow!");
boxes.forEach(box => {
if(Math.random() > 0.5){
box.classList.add('light-on');
}
else{
box.classList.remove('light.on');
}
});
}
homeButton.addEventListener('click', ()=> home());
sizes.forEach((size,index) => {
size.addEventListener('click', ()=> handleSize(index));
});
function handleButton() {
boxes = document.querySelectorAll('.box');
boxes.forEach((box, index) => {
box.addEventListener('click', () => handleClick(index));
});
counterElement.classList.toggle('hidden');
}
function turnOpposite(indexBox){
indexBox.classList.toggle('light-on');
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function handleSize (index){
sizes.forEach((size) => {
size.classList.toggle('hidden');
});
if(index == 0){
gridSize.forEach((grid) => {
grid.classList.toggle('grid3');
});
}
else if(index == 1){
y = 4;
x= 4;
gridSize.forEach((grid) => {
grid.classList.toggle('grid4');
});
}
else if(index == 2){
x = 5;
y= 5;
gridSize.forEach((grid) => {
grid.classList.toggle('grid5');
});
}
for(let i=0; i<(x*y);i++){
const button = document.createElement('button');
button.classList.add('box');
grid.appendChild(button);
}
handleButton();
randomize();
}
function handleClick(index){
turnOpposite(boxes[index]);
if(index >= x) turnOpposite(boxes[index - x]);
if(index <= (x*y - x -1)) turnOpposite(boxes[index +x]);
if(index % x != 0) turnOpposite(boxes[index -1]);
if(index % x != x-1) turnOpposite(boxes[index +1]);
checkWinner();
if(count == 50){
alert("You're at 50 Tries!");
}
else if(count == 100) alert("Passed 100 Tries!");
else if(count == 150) alert("Alright time to switch to another game");
count++;
counterElement.innerHTML = `Total Tries: ${count}`;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function checkWinner() {
let allLit = true;
let on = 0;
let off = 0;
boxes.forEach(box => {
if (!box.classList.contains('light-on')) off++;
else{
on++;
}
});
if(off == 0 ) alert(`Only took you about ${count} Tries !`);
if(on == 0) alert(`You were supposed to turn all lights ON!`);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function home (){
location.reload();
}