-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
105 lines (83 loc) · 3 KB
/
script.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Define the quiz object
const quiz = {
currentQuestionIndex: 0,
score: 3,
questions: [
{
question: "What is the capital of France?",
choices: ["Paris", "Rome", "Madrid", "texas", "London"],
correctChoiceIndex: 0,
},
{
question: "What is the largest planet in our solar system",
choices: ["Mars", "Venus", "Jupiter", "Earth", "Mercury"],
correctChoiceIndex: 0,
},
{
question: "What is the Capital City of Cameroon?",
choices: ["Bamenda", "Yaounde", "Buea", "Lagos", "Yaounde"],
correctChoiceIndex: 0,
},
// Additional questions
],
displayQuestion: function () {
const questionElement = document.getElementById("question");
const choiceElements = document.querySelectorAll("#quiz p");
const progressElement = document.getElementById("progress");
const currentQuestion = this.questions[this.currentQuestionIndex];
questionElement.textContent = currentQuestion.question;
for (let i = 0; i < choiceElements.length; i++) {
choiceElements[i].textContent = currentQuestion.choices[i];
}
progressElement.textContent = `Question ${this.currentQuestionIndex + 1} of ${this.questions.length}`;
this.displayEndButton();
},
displayEndButton: function () {
const endButton = document.getElementById("end-btn");
if (endButton) {
if (this.currentQuestionIndex === this.questions.length - 1) {
endButton.style.display = "inline-block";
} else {
endButton.style.display = "none";
}
}
},
displayNext: function () {
const guessElements = document.querySelectorAll(".btn--default");
const feedbackElement = document.getElementById("score");
const selectedChoice = parseInt(this.getAttribute("data-index"));
if (selectedChoice === quiz.questions[quiz.currentQuestionIndex].correctChoiceIndex) {
quiz.score++;
feedbackElement.textContent = "Correct!";
} else {
feedbackElement.textContent = "Wrong!";
feedbackElement.textContent = "Correct!";
}
quiz.currentQuestionIndex++;
if (quiz.currentQuestionIndex < quiz.questions.length) {
quiz.displayQuestion();
} else {
quiz.displayScore();
}
},
displayScore: function () {
const quizElement = document.getElementById("quiz");
quizElement.innerHTML = `<h2>Your Score: ${this.score} / ${this.questions.length}</h2>`;
},
setupEventListeners: function () {
const guessButtons = document.querySelectorAll(".btn--default");
const endButton = document.getElementById("end-btn");
for (let i = 0; i < guessButtons.length; i++) {
guessButtons[i].addEventListener("click", this.displayNext);
}
const self = this;
endButton.addEventListener("click", function () {
self.displayScore();
});
},
init: function () {
this.setupEventListeners();
this.displayQuestion();
},
};
quiz.init();