-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
59 lines (51 loc) · 1.51 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
55
56
57
58
59
let color = "black";
let click = true;
function populateBoard(size) {
let board = document.querySelector(".board");
let squares = board.querySelectorAll("div");
squares.forEach((div) => div.remove());
board.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
board.style.gridTemplateRows = `repeat(${size}, 1fr)`;
let amount = size * size;
for (let i = 0; i < amount; i++) {
let square = document.createElement("div");
square.addEventListener("mouseover", colorSquare);
square.style.backgroundColor = "white";
board.insertAdjacentElement("beforeend", square);
}
}
populateBoard(16);
function changeSize(input) {
if (input >= 2 && input <= 100) {
populateBoard(input);
} else {
console.log("to many squares");
}
}
function colorSquare() {
if (click) {
if (color === "random") {
this.style.backgroundColor = `hsl(${Math.random() * 360}, 100%, 50%)`;
} else {
this.style.backgroundColor = color;
}
}
}
function changeColor(choice) {
color = choice;
}
function resetBoard() {
let board = document.querySelector(".board");
let squares = board.querySelectorAll("div");
squares.forEach((div) => (div.style.backgroundColor = "white"));
}
document.querySelector("body").addEventListener("click", (e) => {
if (e.target.tagName != "BUTTON") {
click = !click;
if (click) {
document.querySelector(".mode").textContent = "Mode: Coloring";
} else {
document.querySelector(".mode").textContent = "Mode: Not Coloring";
}
}
});