-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (58 loc) · 2.26 KB
/
script.js
File metadata and controls
66 lines (58 loc) · 2.26 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
function getRandomColors() {
const randomColors = [];
while (randomColors.length < 6) {
const randomColor = `rgb(${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)}, ${Math.floor(Math.random() * 256)})`;
if (!randomColors.includes(randomColor)) {
randomColors.push(randomColor);
}
}
return randomColors;
}
let colors = getRandomColors();
let targetColor = "";
let score = 0;
function startGame() {
score = 0; //
document.getElementById("score").textContent = score;
colors = getRandomColors();
setNewTargetColor();
document.getElementById("gameStatus").textContent = "";
generateColorOptions();
}
function setNewTargetColor() {
targetColor = colors[Math.floor(Math.random() * colors.length)];
document.getElementById("colorBox").style.backgroundColor = targetColor;
}
function generateColorOptions() {
const colorOptionsContainer = document.getElementById("colorOptions");
colorOptionsContainer.innerHTML = "";
colors.forEach(color => {
const button = document.createElement("button");
button.classList.add("color-option");
button.style.backgroundColor = color;
button.setAttribute("data-testid", "colorOption");
button.addEventListener("click", () => checkGuess(color));
colorOptionsContainer.appendChild(button);
});
}
function checkGuess(color) {
const gameStatus = document.getElementById("gameStatus");
if (color === targetColor) {
document.getElementById("gameStatus").textContent = "Yayyyy 💃 that's right! Keep going 💪.";
gameStatus.textContent = "Yayyyy that's right! Keep going 💪.";
score++;
document.getElementById("score").textContent = score;
colors = getRandomColors();
setNewTargetColor();
generateColorOptions();
} else {
document.getElementById("gameStatus").textContent = "Oops! Try again 🙂↔️.";
gameStatus.textContent = "Oops! Try again.";
}
gameStatus.style.opacity = "1";
setTimeout(() => {
gameStatus.style.opacity = "0";
}, 1000);
}
document.getElementById("newGameButton").addEventListener("click", startGame);
startGame();