-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
43 lines (32 loc) · 1.06 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
const container = document.querySelector(".container");
const button = document.querySelector(".button");
function changeGridSize() {
let gridSize = prompt("Enter a number to change the grid size!");
while (gridSize < 0 || gridSize > 100 || !gridSize) {
gridSize = prompt("Enter a number from 0 to 100!");
}
while (container.firstChild) {
container.removeChild(container.firstChild);
}
makeGrid(gridSize);
}
function makeGrid(number = 16) {
const container = document.querySelector(".container");
const squarePower = Math.pow(number, 2);
container.style.setProperty("grid-template-rows", `repeat(${number}, 1fr)`);
container.style.setProperty(
"grid-template-columns",
`repeat(${number}, 1fr)`
);
for (let i = 0; i < squarePower; i++) {
let createDiv = document.createElement("div");
createDiv.addEventListener("mouseover", (e) => {
e.target.style.backgroundColor = "black";
});
container.appendChild(createDiv);
}
}
// Load Page
makeGrid();
// Event Listeners
button.addEventListener("click", changeGridSize);