-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (34 loc) · 1.39 KB
/
script.js
File metadata and controls
43 lines (34 loc) · 1.39 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
const colors = ['Red', 'Green', 'Blue', 'Yellow', 'Orange', 'Purple', 'Pink'];
let correctAnswer;
function startGame() {
const randomColorIndex = Math.floor(Math.random() * colors.length);
const correctColor = colors[randomColorIndex];
document.getElementById('colorBox').style.backgroundColor = correctColor.toLowerCase();
// Shuffle options and place one correct answer randomly
let options = shuffleArray([correctColor, getRandomColor(), getRandomColor()]);
for (let i = 0; i < 3; i++) {
document.getElementById(`option${i}`).textContent = options[i];
}
correctAnswer = options.indexOf(correctColor);
document.getElementById('result').textContent = "";
}
function checkAnswer(selectedOption) {
if (selectedOption === correctAnswer) {
document.getElementById('result').textContent = "Correct!";
document.getElementById('result').style.color = "green";
} else {
document.getElementById('result').textContent = "Wrong! Try again.";
document.getElementById('result').style.color = "red";
}
}
function getRandomColor() {
return colors[Math.floor(Math.random() * colors.length)];
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
startGame();