-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (36 loc) · 1.16 KB
/
app.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
let mainContainer = document.getElementById("main-container");
let squareNumber = Number(prompt("Choose grid size between 1-100!", "16"));
function checkValidPrompt(squareNumber) {
while (
squareNumber > 100 ||
squareNumber < 1 ||
!Number.isInteger(squareNumber)
) {
squareNumber = Number(prompt("Enter a valid value!", "16"));
}
}
checkValidPrompt(squareNumber);
let squareSize = mainContainer.clientWidth / squareNumber;
for (let i = 0; i < squareNumber ** 2; i++) {
let gridDiv = document.createElement("div");
gridDiv.setAttribute("class", "square-grid");
mainContainer.appendChild(gridDiv);
gridDiv.style.width = squareSize + "px";
gridDiv.style.height = squareSize + "px";
gridDiv.style.flexBasis = squareSize + "px";
}
let squareDivs = document.querySelectorAll(".square-grid");
squareDivs.forEach((item) => {
item.addEventListener("mousedown", (e) => {
item.style.backgroundColor = "gray";
if (e.buttons == 1) {
squareDivs.forEach((div) =>
div.addEventListener("mouseover", (e) => {
if (e.buttons == 1) {
e.target.style.backgroundColor = "black";
}
})
);
}
});
});