-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
53 lines (45 loc) · 1.3 KB
/
Copy pathjavascript.js
File metadata and controls
53 lines (45 loc) · 1.3 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
let gridContainer = document.getElementById("gridContainer");
function colorChange(element, color) {
element.style.backgroundColor = color;
}
function generateGrid(size) {
for (let i = 0; i < size; i++) {
let row = document.createElement("div");
gridContainer.appendChild(row);
row.className = "row";
for (let e = 0; e < size; e++) {
let column = document.createElement("div");
row.appendChild(column);
column.className = "column";
column.addEventListener("mouseover", () =>
colorChange(column, randomRbg())
);
}
}
}
function clearGrid() {
while (gridContainer.firstChild) {
gridContainer.removeChild(gridContainer.firstChild);
}
}
let newGrid = document.getElementById("newGrid");
newGrid.addEventListener("click", function () {
let size = parseInt(
prompt("What number of squares per side would you like?"),
10
);
if (isNaN(size) || size < 1 || size > 100) {
alert("Please enter a number between 1 and 100.");
return;
}
clearGrid();
generateGrid(size);
});
// Initial grid creation on page load
generateGrid(16);
function randomRbg() {
let red = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
return `rgb(${red} ,${blue} ,${green})`;
}