`; q.options.forEach((opt, i) => { html += `
${opt}
`; }); html += `
Time Left: ${timeLeft}s
`; document.getElementById("quiz-container").innerHTML = html; startTimer(); } function startTimer() { timeLeft = 20; clearInterval(timer); timer = setInterval(() => { document.getElementById("timer").innerText = "Time Left: " + timeLeft + "s"; if (timeLeft-- <= 0) { clearInterval(timer); showCorrect(null); // time out → show correct setTimeout(nextQuestion, 1500); } }, 1000); } function checkAnswer(element, i) { clearInterval(timer); if (i === quizData[current].a) { score++; element.classList.add("correct"); document.getElementById("correct-sound").play(); } else { element.classList.add("wrong"); document.getElementById("wrong-sound").play(); showCorrect(i); // highlight correct even if wrong } setTimeout(nextQuestion, 1500); } function showCorrect(selectedIndex) { const options = document.querySelectorAll(".option"); options.forEach((opt, idx) => { if (idx === quizData[current].a) { opt.classList.add("correct"); } }); } function nextQuestion() { current++; loadQuestion(); } function showAnswers() { let ansDiv = document.getElementById("answers"); ansDiv.innerHTML = "
Step-by-Step Answers
"; quizData.forEach((q, i) => { ansDiv.innerHTML += `
Q${i+1}: ${q.q}
Answer: ${q.options[q.a]}
Steps: ${q.steps}
`; }); } loadQuestion();