-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorGame.js
More file actions
106 lines (92 loc) · 2.72 KB
/
ColorGame.js
File metadata and controls
106 lines (92 loc) · 2.72 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
98
99
100
101
102
103
104
105
106
let squares = document.querySelectorAll(".square");
let RGB = document.querySelector("#RGB");
let message = document.querySelector("#message");
let h1 = document.querySelector("h1");
let reset = document.querySelector("#reset");
let modeButtons = document.querySelectorAll(".mode");
//All Logic
let numberSquares = 6;
let colors = [];
let pickedColor;
//first running
init();
function init() {
setupModeButtons();
setupSquares();
resetFunc();
}
reset.addEventListener("click", function() {
resetFunc();
});
function changeColors(color) {
for (let i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = color;
}
}
function pickColor() {
let random = Math.floor(Math.random() * colors.length);
return colors[random];
}
function generateRandomColors(length) {
let arrayOfCollors = [];
for (let i = 0; i < length; i++) {
arrayOfCollors.push(randomColor());
}
return arrayOfCollors;
}
function randomColor() {
let r = randomNumber();
let g = randomNumber();
let b = randomNumber();
return "rgb(" + r + ", " + g + ", " + b + ")";
}
function randomNumber() {
let random = Math.floor(Math.random() * 255 + 1);
return random;
}
function resetFunc() {
colors = generateRandomColors(numberSquares);
pickedColor = pickColor();
RGB.textContent = pickedColor;
h1.style.backgroundColor = "#78cff9";
reset.textContent = "New colors";
message.textContent = "";
for (let i = 0; i < squares.length; i++) {
if (colors[i]) {
squares[i].style.display = "block";
squares[i].style.backgroundColor = colors[i];
} else {
squares[i].style.display = "none";
}
}
h1.style.backgroundColor = "#78cff9";
}
function setupModeButtons(){
for (let i = 0; i < modeButtons.length; i++) {
modeButtons[i].addEventListener("click", function() {
modeButtons[0].classList.remove("selected");
modeButtons[1].classList.remove("selected");
this.classList.add("selected");
this.textContent === "Easy" ? (numberSquares = 3) : (numberSquares = 6);
resetFunc();
});
}
}
function setupSquares(){
for (let i = 0; i < squares.length; i++) {
squares[i].style.backgroundColor = colors[i];
squares[i].addEventListener("click", function() {
let choosedColor = this.style.backgroundColor;
message.textContent = "Correct";
if (choosedColor === pickedColor) {
changeColors(pickedColor);
message.textContent = "Correct!";
h1.style.backgroundColor = pickedColor;
reset.textContent = "Play again?";
} else {
message.textContent = "Try again";
this.style.backgroundColor = "#232323";
}
});
}
}