-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (37 loc) · 1.6 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
const MAX_SKETCH_PAD_WIDTH = 960;
const STARTING_NUM_OF_SQUARES_PER_SIDE = 16;
function createGrid(numOfSquaresPerSide) {
const grid = document.getElementById('grid');
for (let i = 0; i < numOfSquaresPerSide; i++) {;
for (let j = 0; j < numOfSquaresPerSide; j++) {
const currentSquareDiv = document.createElement('div');
let currentSquareDivMeasurement = MAX_SKETCH_PAD_WIDTH / numOfSquaresPerSide
currentSquareDiv.style.width = currentSquareDiv.style.height = `${currentSquareDivMeasurement}px`;
currentSquareDiv.addEventListener("mouseover", () => {
currentSquareDiv.style.backgroundColor = "darkolivegreen";
});
grid.appendChild(currentSquareDiv);
}
}
}
/*
This helper function clears the current etch-a-sketch pad/grid that's on the
page. Mainly added to declutter 'main' & organize code for newer features later on.
*/
function clearCurrentGrid() {
const grid = document.getElementById('grid');
grid.innerHTML = "";
}
function main() {
const newGridButton = document.querySelector('.new-grid-button');
newGridButton.addEventListener("click", () => {
let userGridPrompt = parseInt(prompt("Enter the number of squares per side"));
while ((userGridPrompt > 100 || userGridPrompt < 1) || isNaN(userGridPrompt)) {
userGridPrompt = parseInt(prompt("Enter the number of squares per side"));
}
clearCurrentGrid();
createGrid(userGridPrompt);
});
createGrid(STARTING_NUM_OF_SQUARES_PER_SIDE);
}
main()