-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (26 loc) · 1.01 KB
/
Copy pathscript.js
File metadata and controls
29 lines (26 loc) · 1.01 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
const gameContainer = document.getElementById('game-container');
const scoreDisplay = document.getElementById('score-display'); // spelling fix
const colors = ['red', 'blue', 'green', 'yellow'];
let score = 0;
function createColorBox(color) {
const box = document.createElement('div');
box.classList.add('color-box');
box.style.backgroundColor = color;
box.addEventListener('click', checkColor);
gameContainer.appendChild(box);
return box;
}
function checkColor(event) {
const clickedBox = event.target;
const randomColor = colors[Math.floor(Math.random() * colors.length)]; // () fix
if (clickedBox.style.backgroundColor === randomColor) {
score++;
scoreDisplay.textContent = `Score: ${score}`; // template fix
alert(`YAY! You matched the color! 🎉`);
} else {
alert(`Oops! The color was ${randomColor}. Try Again 😊`);
}
}
for (let i = 0; i < colors.length; i++) { // length spelling fix
createColorBox(colors[i]);
}