forked from TONYSTRAK25/QUIZ-PROJECT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (64 loc) · 2.13 KB
/
Copy pathscript.js
File metadata and controls
76 lines (64 loc) · 2.13 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
67
68
69
70
71
72
73
74
75
76
#check here upstream
const questionContainer = document.getElementById("question-container");
const questionElement = document.getElementById("question");
const answerButtons = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-btn");
const scoreDisplay = document.getElementById("score-display");
let currentQuestionIndex = 0;
let score = 0;
function startQuiz() {
currentQuestionIndex = 0;
score = 0;
nextButton.classList.add("hidden");
showQuestion();
}
function showQuestion() {
resetState();
let currentQuestion = questions[currentQuestionIndex];
questionElement.innerText = currentQuestion.question;
currentQuestion.answers.forEach(answer => {
const button = document.createElement("button");
button.innerText = answer.text;
button.classList.add("answer-btn");
button.addEventListener("click", () => selectAnswer(answer, button));
answerButtons.appendChild(button);
});
}
function resetState() {
nextButton.classList.add("hidden");
answerButtons.innerHTML = "";
}
function selectAnswer(answer, button) {
const correct = answer.correct;
if (correct) {
button.classList.add("correct");
score++;
} else {
button.classList.add("wrong");
}
Array.from(answerButtons.children).forEach(btn => {
btn.disabled = true;
});
nextButton.classList.remove("hidden");
}
nextButton.addEventListener("click", () => {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
} else {
showScore();
}
});
function showScore() {
questionContainer.classList.add("hidden");
scoreDisplay.innerText = `Your Score: ${score} / ${questions.length}`;
scoreDisplay.classList.remove("hidden");
nextButton.innerText = "Restart";
nextButton.addEventListener("click", () => {
scoreDisplay.classList.add("hidden");
questionContainer.classList.remove("hidden");
nextButton.innerText = "Next";
startQuiz();
});
}
startQuiz();