-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (106 loc) · 3.39 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const rgbHeader = document.querySelector("#rgbHeader");
const colorBoxes = [...document.querySelectorAll(".colorBoxes")];
const headerContainer = document.querySelector(".headerContainer");
const newColors = document.querySelector("#newColors");
const lowerColorBoxes = document.querySelector("#lowerColorBoxes");
const easyMode = document.querySelector("#easy");
const hardMode = document.querySelector("#hard");
//test
////////////////////////////////////////////////////////////////
function random255() {
return Math.floor(Math.random() * 256);
}
// create a set of random numbers
function randomColor() {
const red = random255();
const green = random255();
const blue = random255();
return `${red}, ${green}, ${blue}`;
}
// Create an array of set of random numbers
function arrColors(num) {
const arrColors = [];
for (let i = 0; i < num; i++) {
arrColors[i] = `${randomColor()}`;
}
return arrColors;
}
function newGame(num) {
const arrColorsSet = arrColors(num);
return arrColorsSet;
}
function pickedColor() {
const randomPick = Math.floor(Math.random() * arrColorsSet.length);
const pickedColor = `rgb(${arrColorsSet[randomPick]})`;
return pickedColor;
}
function generateColors() {
for (let i = 0; i < colorBoxes.length; i++) {
colorBoxes[i].style.background = `rgb(${arrColorsSet[i]})`;
}
}
function getNewColor() {
return (rgbHeader.innerHTML = `${theColor}`);
}
let arrColorsSet = newGame(6);
let theColor = pickedColor();
getNewColor();
generateColors();
// Click Color Event that will find out if it's the correct color
////////////////////////////////////////////////////////////////
const clickedColor = colorBoxes.map((box) => {
box.addEventListener("click", function () {
const clickedColorVal = this.style.backgroundColor;
const pickedColorVal = theColor;
console.log(pickedColorVal);
console.log(clickedColorVal);
if (clickedColorVal === pickedColorVal) {
return colorBoxes.map((box) => {
const correctBGColor = clickedColorVal;
box.style = `background-color: ${clickedColorVal}; opacity: 1;`;
headerContainer.style = `background-color: ${clickedColorVal};`;
});
} else {
return (this.style = "background: #232323; opacity: 0;");
}
});
});
////////////////////////////////////////////////////////////////
// Functions for Click Events
////////////////////////////////////////////////////////////////
function backToDefault() {
theColor = pickedColor();
getNewColor();
generateColors();
headerContainer.style = `background-color: #067e82;`;
}
function easyModeOn() {
arrColorsSet = newGame(3);
lowerColorBoxes.style.display = "none";
backToDefault();
}
function hardModeOn() {
arrColorsSet = newGame(6);
lowerColorBoxes.style.display = "flex";
backToDefault();
}
// Click Event Handlers
////////////////////////////////////////////////////////////////
newColors.addEventListener("click", () => {
if (easyMode.classList.contains("selectedMode")) {
return easyModeOn();
} else {
return hardModeOn();
}
});
easyMode.addEventListener("click", () => {
easyMode.classList.add("selectedMode");
hardMode.classList.remove("selectedMode");
return easyModeOn();
});
hardMode.addEventListener("click", () => {
hardMode.classList.add("selectedMode");
easyMode.classList.remove("selectedMode");
return hardModeOn();
});
////////////////////////////////////////////////////////////////