-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
159 lines (148 loc) · 4.92 KB
/
main.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
function blackColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'black';
}
}
}
function redColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'red';
}
}
}
function yellowColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'yellow';
}
}
}
function blueColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'blue';
}
}
}
function greenColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'green';
}
}
}
function orangeColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'orange';
}
}
}
function purpleColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
this.style.backgroundColor = 'purple';
}
}
}
function randomColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].onmouseover = function (e) {
const rndCol = '#' + Math.floor(Math.random() * 16777215).toString(16);
this.style.backgroundColor = rndCol;
}
}
}
function eraseColour() {
let squares = document.getElementsByClassName('square');
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = "";
}
}
function squareGrid(x, y) {
let container1 = document.getElementById('container1');
container1.style.setProperty('--grid-rows', x);
container1.style.setProperty('--grid-cols', y);
for (let i = 0; i < x * y; i++) {
let grid = container1.appendChild(document.createElement("div"));
grid.className = "square";
blackColour();
}
}
squareGrid(16, 16);
function changeGrid() {
let x = prompt("***Note: Larger numbers take longer to process.\n\nHow many squares per side (1 - 100): ");
if (isNaN(x)) {
alert("That's not a valid entry. Try again");
} else {
let container1 = document.getElementById('container1');
let container2 = document.getElementById('container2');
let squares = document.getElementsByClassName('square');
for (let y = 0; y < x * x; y++) {
container1.remove();
let container3 = document.createElement('div')
container3.id = "container2";
container2.style.display = 'grid';
container2.style.height = '514px';
container2.style.width = '514px';
container2.style.margin = '0 auto';
container2.style.border = '1px solid gray';
container2.style.marginTop = '-11%';
container2.style.gridTemplateColumns = 'repeat(var(--grid-cols), 1fr)';
container1.style.gridTemplateColumns = 'repeat(var(--grid-rows), 1fr)';
container2.style.setProperty('--grid-rows', x);
container2.style.setProperty('--grid-cols', x);
let grid = container2.appendChild(document.createElement("div"));
grid.className = "square";
blackColour();
}
}
}
let x = document.querySelectorAll('button');
x.forEach(button => {
button.addEventListener('click', () => {
let selection = button.innerHTML;
switch (selection) {
case "CLEAR GRID":
eraseColour();
break;
case "RANDOM COLOURS":
randomColour();
break;
case "DEFAULT COLOUR":
blackColour();
break;
case "RED":
redColour();
break;
case "YELLOW":
yellowColour();
break;
case "BLUE":
blueColour();
break;
case "GREEN":
greenColour();
break;
case "ORANGE":
orangeColour();
break;
case "PURPLE":
purpleColour();
break;
case "CHANGE GRID SIZE":
changeGrid();
break
}
});
});