-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.js
70 lines (57 loc) · 2.11 KB
/
code.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
60
61
62
63
64
65
66
67
68
69
70
let totalGrids = 16;
function clearPad() {
while (container.hasChildNodes()) {
container.removeChild(container.firstChild);
}
}
function createPad() {
const container = document.querySelector('#container');
const descrip = document.querySelector("#description");
descrip.textContent = `A colorful Etch-A-Sketch in a ${totalGrids} X ${totalGrids} grid`;
for (x = 0; x < (totalGrids * totalGrids); x++) {
const div = document.createElement('div');
div.setAttribute('class', "grid-cells");
container.appendChild(div);
container.style.gridTemplateColumns = `repeat(${totalGrids}, 1fr)`;
container.style.gridTemplateRows = `repeat(${totalGrids}, 1fr)`;
}
}
function gridFunctionality() {
const grids = document.querySelectorAll(".grid-cells");
grids.forEach((grid) => {
let randomColor = Math.floor(Math.random()*16777215).toString(16);
grid.addEventListener('mouseover', (e) => {
e.target.style.background = '#' + randomColor;
});
});
}
//select all the grids and add EventListener whenever
//mouse hoovers over.
createPad();
gridFunctionality();
//create functionality for clear button
const grids = document.querySelectorAll(".grid-cells");
const clearBtn = document.querySelector("#clear-btn");
clearBtn.addEventListener('click', () => {
grids.forEach((grid) => {
grid.style.backgroundColor = 'white';
});
totalGrids = prompt(
"Please specify how many grids per side? (i.e. a choice of '5' will produce a 5x5 pad)");
if (isNaN(parseInt(totalGrids)) === false) {
totalGrids = parseInt(totalGrids);
if (totalGrids > 0) {
clearPad();
createPad();
gridFunctionality();
} else if (totalGrids < 0) {
alert("Invalid input: Please type in a number value over 0");
}
} else {
//handle whether string is inputted or if user clicks cancel
if (!totalGrids) {
} else {
alert("Invalid text input: Please type in a number value");
}
}
});