-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
107 lines (83 loc) · 2.79 KB
/
script.js
File metadata and controls
107 lines (83 loc) · 2.79 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
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
const NUM_ROWS = 5;
const NUM_COLS = 5;
const LIGHTS_OFF_COLOR = 'gray';
const LIGHTS_ON_COLOR = 'orange';
const grid = []
const gameTableEl = document.getElementById('game-table');
function initializeGameTable() {
for (let row = 0; row < NUM_ROWS; row++) {
const tableRowEl = document.createElement('tr');
for (let col = 0; col < NUM_COLS; col++) {
const lightNumber = (row * NUM_ROWS) + col;
const tableDataEl = document.createElement('td');
tableDataEl.style.backgroundColor = LIGHTS_OFF_COLOR;
tableDataEl.setAttribute('id', lightNumber.toFixed());
tableRowEl.appendChild(tableDataEl);
grid.push(tableDataEl);
}
gameTableEl.appendChild(tableRowEl);
}
}
function randomizeLights(){
for(eachLight of grid){
if( Math.random() < 0.5 ){
toggleSingleLight(eachLight);
}
}
}
/**
* Check if all lights are ON!
*/
function checkWin() {
const offLights = grid.filter( (eachLightEl) => {
return eachLightEl.style.backgroundColor === LIGHTS_OFF_COLOR;
});
if(offLights.length === 0){
alert('YOU WIN!');
}
}
/**
* Toggle all lights and check if you won!
* @param {object} event
*/
function toggleLights(event) {
const clickedLightCellEl = event.target;
const lightNumber = parseInt(clickedLightCellEl.getAttribute('id'));
// Toggle color for selected td element
toggleSingleLight(grid[lightNumber]);
// NOT first row, toggle color for TOP td element
if(lightNumber >= NUM_COLS){
toggleSingleLight(grid[lightNumber - NUM_COLS]);
}
// NOT right edge, toggle color for RIGHT td element
if(lightNumber % NUM_COLS !== (NUM_COLS - 1) ){
toggleSingleLight(grid[lightNumber + 1]);
}
// NOT on left edge, toggle color for LEFT td element
if(lightNumber % 5 !== 0){
toggleSingleLight(grid[lightNumber - 1]);
}
// NOT last row, toggle color for BOTTOM td element
if(lightNumber + NUM_COLS <= (NUM_COLS * NUM_ROWS) - 1){
toggleSingleLight(grid[lightNumber + NUM_COLS]);
}
checkWin();
}
/**
* Toggle a single light on/off
* @param {element object} lightCellEl
*/
function toggleSingleLight(lightCellEl) {
const style = lightCellEl.style;
const color = style.backgroundColor;
style.backgroundColor = (color === LIGHTS_ON_COLOR) ? LIGHTS_OFF_COLOR : LIGHTS_ON_COLOR;
}
document.addEventListener('DOMContentLoaded', () => {
const lightElements = document.querySelectorAll('td');
for (let i = 0; i < lightElements.length; i++) {
lightElements[i].addEventListener('click', toggleLights);
}
const button = document.querySelector('button');
button.addEventListener('click', randomizeLights);
});
initializeGameTable();