-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorgame.js
97 lines (87 loc) · 3 KB
/
colorgame.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
let numSquare = 6;
let colors = randomColor(numSquare);
let pickedColor = pickColor();
let squares = document.querySelectorAll(".square");
let colorDisplay = document.getElementById("colorDisplay");
let messageDisplay = document.getElementById("message");
let h1 = document.querySelector("h1");
let reset = document.querySelector("#reset");
let easyBtn = document.querySelector("#easyBtn");
let hardBtn = document.querySelector("#hardBtn");
easyBtn.addEventListener("click", function () {
messageDisplay.textContent = "";
easyBtn.classList.add("selected");
hardBtn.classList.remove("selected");
let numSquare = 3;
colors = randomColor(numSquare);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for (let i = 0; i < squares.length; i++) {
if (colors[i]) {
squares[i].style.background = colors[i];
}
else {
squares[i].style.display = "none";
}
}
});
hardBtn.addEventListener("click", function () {
messageDisplay.textContent = "";
hardBtn.classList.add("selected");
easyBtn.classList.remove("selected");
colors = randomColor(numSquare);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
for (let i = 0; i < squares.length; i++) {
squares[i].style.background = colors[i];
squares[i].style.display = "block";
}
});
reset.addEventListener("click", function () {
colors = randomColor(numSquare);
pickedColor = pickColor();
colorDisplay.textContent = pickedColor;
messageDisplay.textContent = "";
reset.textContent = "Change Color"
for (let i = 0; i < squares.length; i++) {
squares[i].style.background = colors[i];
h1.style.background = " rgb(132, 124, 124)";
}
});
colorDisplay.textContent = pickedColor;
for (let i = 0; i <= squares.length; i++) {
squares[i].style.background = colors[i];
hardBtn.classList.add("selected");
squares[i].addEventListener("click", function () {
let clickedColor = this.style.background;
if (clickedColor === pickedColor) {
messageDisplay.textContent = "Correct!"
reset.textContent = "Play Again?"
for (let i = 0; i <= squares.length; i++) {
squares[i].style.background = pickedColor;
h1.style.background = pickedColor;
}
} else {
this.style.background = "black"
messageDisplay.textContent = "Try Again!"
reset.textContent = "Play Again?"
}
});
}
function pickColor() {
let randomIndex = Math.floor(Math.random() * colors.length);
return colors[randomIndex];
}
function randomColor(num) {
arr = []
for (let i = 0; i < num; i++) {
arr.push(generateRandom());
}
return arr;
}
function generateRandom() {
let red = Math.floor(Math.random() * 256);
let green = Math.floor(Math.random() * 256);
let blue = Math.floor(Math.random() * 256);
return "rgb(" + red + ", " + green + ", " + blue + ")";
}