-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavaScript.js
More file actions
97 lines (81 loc) · 2.95 KB
/
javaScript.js
File metadata and controls
97 lines (81 loc) · 2.95 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const gridContainer = document.querySelector("#grid-container");
window.addEventListener("load", setDefaultGrid);
function setDefaultGrid() {
setGridSize(9);
fillGrid(9);
}
function setGridSize(size) {
gridContainer.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
}
function fillGrid(size) {
for (let i = 0; i < size * size; i++) {
const gridElement = document.createElement("div");
gridElement.classList = "grid-element";
gridElement.addEventListener('mouseover', changeColor)
gridContainer.appendChild(gridElement);
}
}
let browns = ['#D29B74', '#C88556', '#B9703C', '#9A5D32', '#7B4B28', '6C4123', '4D2F19']
function changeColor(e) {
console.log( "brown colors");
let rand6 = Math.floor(Math.random() * 6)
e.target.style.backgroundColor = browns[rand6]
}
function changeColors(e) {
console.log( "rainbow colors");
const randomR = Math.floor(Math.random() * 256);
const randomG = Math.floor(Math.random() * 256);
const randomB = Math.floor(Math.random() * 256);
e.target.style.backgroundColor = `rgb(${randomR}, ${randomG}, ${randomB})`;
}
const button = document.querySelector("#reset-button");
button.addEventListener("click", askGrid)
function askGrid() {
let newSize = prompt("Enter new size below 35 and above 1");
if (newSize !== null) {
newSize = parseInt(newSize);
if (newSize < 2 || newSize > 35 || Number.isNaN(newSize)) {
alert("Enter a between 1 and 35");
askGrid();
} else {
clearGrid();
setGridSize(newSize);
fillGrid(newSize);
}
}
}
function clearGrid() {
const gridArray = Array.from(gridContainer.childNodes);
gridArray.forEach((element) => {
gridContainer.removeChild(element);
});
}
function cleanGrid() {
const elements = document.querySelectorAll(".grid-element")
for (const iterator of elements) {
iterator.style.backgroundColor = "#e6cab6";
}
}
const buttonClear = document.querySelector("#clean-button")
buttonClear.addEventListener('click', cleanGrid)
//-------------------------- HERE BE BUGS ==================
const buttonColors = document.querySelector("#colors-button")
buttonColors.addEventListener('click', moveColors)
function moveColors() {
const elements = document.querySelectorAll(".grid-element")
for (const iterator of elements) {
iterator.addEventListener('mouseover', changeColors)
iterator.removeEventListener('mouseover', changeColor)
}
}
const buttonBrowns = document.querySelector("#brown-button")
buttonBrowns.addEventListener('click', moveBrown)
function moveBrown() {
console.log('Hello from browns');
const elements = document.querySelectorAll(".grid-element")
for (const iterator of elements) {
console.log('debuggins from from browns');
iterator.addEventListener('mouseover', changeColor)
iterator.removeEventListener('mouseover', changeColors)
}
}