-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
46 lines (31 loc) · 1.12 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
//Generate an n * n set of divs given input of n
function createSquares(numRows) {
const container = document.querySelector('.main-container');
const numSquares = numRows ** 2;
for (let i = 0; i < numSquares; i++) {
const newSquare = document.createElement('div');
newSquare.classList.add('grid-square');
container.appendChild(newSquare);
}
// Change colour when mouse enters square
const gridSquares = document.querySelectorAll('.grid-square');
gridSquares.forEach((square) => {
square.addEventListener('mouseenter', setSquareVisited);
}
);
}
function setSquareVisited (event) {
event.target.classList.add('visited');
}
//Create the sketching area
createSquares(16);
//Call createSquares when button clicked
const gridSizeButton = document.querySelector('.set-grid-size');
gridSizeButton.addEventListener('click', () => {
const gridSize = prompt('How many Rows?');
if (gridSize < 100 && gridSize > 1) {
const container = document.querySelector('.main-container');
container.textContent = '';
createSquares(gridSize);
}
})