-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
45 lines (37 loc) · 1.36 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
let defaultColor = "white";
let currentColor = "black";
document.addEventListener("DOMContentLoaded", function() {
const grid = document.getElementById("grid");
console.log(grid);
createSquares(16);
const reset = document.querySelector("#reset");
reset.addEventListener("click", newCanvas);
});
const newCanvas = function(){
let grid = document.getElementById("grid");
do{
var selection = parseInt(window.prompt("How many squares long?", ""), 10);
}while(isNaN(selection) || selection > 100 || selection < 1);
numSquares = selection;
while (grid.firstChild) {
grid.removeChild(grid.firstChild);
}
createSquares(numSquares);
}
const createSquares = function(numSquares) {
for(let i = 0; i < numSquares; i++){
for(let j = 0; j < numSquares; j++){
const div = document.createElement('div');
div.classList.add('square');
div.style.backgroundColor = defaultColor;
div.style.height =`${960/numSquares}px`;
div.style.width = `${960/numSquares}px`;;
grid.appendChild(div);
console.log("square created");
div.addEventListener("mouseover", () => {
div.style.backgroundColor = "#"+Math.floor(Math.random()*16777215).toString(16);
console.log("color changed")
});
}
}
}