-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (52 loc) · 1.4 KB
/
Copy pathscript.js
File metadata and controls
60 lines (52 loc) · 1.4 KB
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
const container = document.querySelector('#container');
const btn = document.querySelector('#reset');
const inpt = document.querySelector('#input');
const rainbow = document.querySelector('#rainbow')
let input;
function getVal() {
input = parseInt(inpt.value);
if(input > 100) {
input = 100;
return input;
} else if (input === undefined || isNaN(input)) {
input = 10;
return input;
}
return input;
}
function reset() {
btn.addEventListener('click', () => {
container.innerHTML = '';
console.log(input);
getVal();
makeGrid(input);
});
}
function makeGrid(grid) {
for (i = 0; i < grid; i++) {
let row = document.createElement('div');
row.classList.add('row');
container.appendChild(row);
for (j = 0; j < grid; j++) {
let box = document.createElement('div');
box.classList.add('box');
row.appendChild(box);
}
}
}
container.addEventListener('mouseover', function(event) {
if (rainbow.checked === true) {
let colorful = getRandomColor();
event.target.style.backgroundColor = `${colorful}`;
} else if (rainbow.checked === false) {
event.target.style.backgroundColor = '#999';
}
});
function getRandomColor() {
let r = Math.floor(Math.random() * 256 + 1);
let g = Math.floor(Math.random() * 256 + 1);
let b = Math.floor(Math.random() * 256 + 1);
return `rgb(${r}, ${g}, ${b})`;
}
reset();
makeGrid(10);