-
Notifications
You must be signed in to change notification settings - Fork 0
/
etchasketch.js
59 lines (42 loc) · 1.44 KB
/
etchasketch.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
const grid = document.querySelector('.sketch');
const resetButton = document.querySelector('.reset');
const sizeButton = document.querySelector('.changeSize');
const rowDiv = [];
const colDiv = [];
let size = 16;
function createGrid(dimension = 16) {
deleteGrid();
for (let i = 0; i < dimension; i++) {
rowDiv[i] = document.createElement('div');
grid.appendChild(rowDiv[i]);
rowDiv[i].classList.add('row','container')
for (let x = 0; x < dimension; x++) {
colDiv[x] = document.createElement('div');
rowDiv[i].appendChild(colDiv[x]);
colDiv[x].classList.add('cell');
colDiv[x].addEventListener('mouseover', function(e) {
e.target.classList.add('hovered');
});
}
}
console.log(dimension);
}
function deleteGrid() {
if (rowDiv.length > 0) {
for (let i = rowDiv.length - 1; i >= 0; i--) {
// rowDiv[i].remove();
grid.firstElementChild.remove();
}
}
}
function resetGrid() {
createGrid(size);
}
resetButton.addEventListener('click', resetGrid);
sizeButton.addEventListener('click', function (e) {
size = prompt("Please enter a positive number between 2 and 100:");
// while (!(size >= 2 && size <= 100)) {
// size = prompt("Please try again. Enter a positive number between 2 and 100:");
// }
createGrid(size);
});