-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (48 loc) · 1.94 KB
/
script.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
function drawGrid(gridSize) {
while (rowCount < gridSize) {
const row = document.createElement('div');
row.classList.add('row');
while (boxCount < gridSize) {
const box = document.createElement('div');
box.classList.add('box');
row.appendChild(box);
boxCount++
}
container.appendChild(row);
rowCount++
boxCount = 0
};
rowCount = 0
createEvent();
}
function createEvent() {
const boxes = document.querySelectorAll('.box') // create Node List containing all boxes
boxes.forEach((box => { // loop through boxes
box.addEventListener('mouseover', function() { // add event listener
box.style.backgroundColor = colorGen();
});
}));
}
function resetGrid() { // reset the grid and asks the user for a new grid size
const rows = document.querySelectorAll('.row') // create a Node List containing all 8 rows
rows.forEach((row => { // loop through each row within rows
container.removeChild(row); // remove each row from the container
}))
let newGridSize = prompt('What grid size would you like to play with next? (1 - 99)'); // ask the user for new gridSize
while (newGridSize >= 100 || newGridSize <= 0) {
newGridSize = prompt('Please select a number between 1 and 99!')
}
drawGrid(newGridSize) // draw new grid using newGridSize
};
function colorGen() { // return random RGB color
const r = Math.floor(Math.random() * 256);
const g = Math.floor(Math.random() * 256);
const b = Math.floor(Math.random() * 256);
return "rgb(" + r + "," + g + "," + b + ")";
}
const container = document.querySelector('#container'); // add container to variable
const reset = document.querySelector('#reset'); // add reset button to reset variable
let rowCount = 0
let boxCount = 0
drawGrid(16)
reset.addEventListener('click', resetGrid);