-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (46 loc) · 1.63 KB
/
script.js
File metadata and controls
55 lines (46 loc) · 1.63 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
let current = 0;
let score = 0;
function showQuestion() {
const q = quiz[current];
document.getElementById("question").innerText = q.question;
const optionsDiv = document.getElementById("options");
optionsDiv.innerHTML = "";
document.getElementById("explanation").innerText = "";
document.getElementById("result").innerText = "";
q.options.forEach(opt => {
const btn = document.createElement("button");
btn.innerText = opt;
btn.onclick = () => checkAnswer(opt);
optionsDiv.appendChild(btn);
});
}
function checkAnswer(selected) {
const correct = quiz[current].answer;
const explanation = quiz[current].explanation;
if (selected === correct) {
score++;
document.getElementById("result").innerText = "✅ Correct!";
document.getElementById("result").style.color = "lightgreen";
} else {
document.getElementById("result").innerText = `❌ Wrong! Correct: ${correct}`;
document.getElementById("result").style.color = "tomato";
}
document.getElementById("explanation").innerText = `💡 ${explanation}`;
// Disable all options
Array.from(document.getElementById("options").children).forEach(btn => {
btn.disabled = true;
});
}
function nextQuestion() {
current++;
if (current < quiz.length) {
showQuestion();
} else {
document.getElementById("question").innerText = "🎉 Quiz Finished!";
document.getElementById("options").innerHTML = "";
document.getElementById("result").innerText = `Score: ${score}/${quiz.length}`;
document.getElementById("explanation").innerText = "";
document.getElementById("next-btn").style.display = "none";
}
}
showQuestion();