-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
108 lines (99 loc) · 2.94 KB
/
app.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
var squares = document.querySelectorAll(".squares");
var colorDisplay = document.getElementById("colorDisplay");
var message = document.getElementById("message");
var easyBtn = document.getElementById("easyBtn");
var hardBtn = document.getElementById("hardBtn");
var resetBtn = document.getElementById("reset");
var h1 = document.querySelector("h1");
var colors = [];
var pickedColor;
function randomNumber() {
var num1 = Math.floor(Math.random() * 256);
var num2 = Math.floor(Math.random() * 256);
var num3 = Math.floor(Math.random() * 256);
return `rgb(${num1}, ${num2}, ${num3})`;
}
function randomColor(num) {
for (var i = 0; i < num; i++) {
colors.push(randomNumber());
randomNumber();
}
}
function pickColor(num) {
pickedColor = colors[Math.floor(Math.random() * num)];
colorDisplay.textContent = pickedColor;
}
function changeColor() {
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = pickedColor;
}
h1.style.backgroundColor = pickedColor;
}
function assignColors() {
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
}
for (var i = 0; i < squares.length; i++) {
squares[i].addEventListener("click", function () {
if (this.style.backgroundColor === pickedColor) {
changeColor();
message.textContent = "Bingo!";
resetBtn.textContent = "Play Again?";
} else {
this.style.backgroundColor = "rgb(35, 35, 35)";
message.textContent = "Try again";
}
});
}
}
randomColor(6);
pickColor(6);
assignColors();
easyBtn.addEventListener("click", function () {
colors = [];
randomColor(3);
pickColor(3);
assignColors();
for (var i = 0; i < squares.length; i++) {
if (!colors.includes(squares[i].style.backgroundColor)) {
squares[i].style.backgroundColor = "rgb(35, 35, 35)";
squares[i].style.display = "none";
}
}
h1.style.backgroundColor = "steelblue";
this.classList.add("selected");
hardBtn.classList.remove("selected");
resetBtn.textContent = "New Colors";
message.textContent = ""
});
hardBtn.addEventListener("click", function () {
colors = [];
randomColor(6);
pickColor(6);
assignColors();
for (var i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].style.display = "block";
}
h1.style.backgroundColor = "steelblue";
this.classList.add("selected");
easyBtn.classList.remove("selected");
resetBtn.textContent = "New Colors";
message.textContent = ""
});
resetBtn.addEventListener("click", function () {
if (colors.length === 3) {
colors = [];
randomColor(3);
pickColor(3);
assignColors();
} else {
colors = [];
randomColor(6);
pickColor(6);
assignColors();
}
h1.style.backgroundColor = "steelblue";
this.textContent = "New Colors";
message.textContent = ""
});