-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
214 lines (182 loc) · 5.94 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Define variables
let currentQuestion = 0;
let previousQuestion = 0;
// let previousQuestion = -1;
let score = 0;
let timer;
// Define DOM elements
const quiz = document.getElementById("quiz");
const questionEl = document.getElementById("question");
const option1El = document.getElementById("option1_label");
const option2El = document.getElementById("option2_label");
const option3El = document.getElementById("option3_label");
const option4El = document.getElementById("option4_label");
const startBtn = document.getElementById("start");
const nextBtn = document.getElementById("next");
const prevBtn = document.getElementById("previous");
const submitBtn = document.getElementById("submit");
const timerEl = document.getElementById("timer");
const resultEl = document.getElementById("result");
const scoreEl = document.getElementById("score");
const options = document.getElementById("options");
const nameT = document.getElementById("name-tag");
// const username = document.getElementById("username");
// const password = document.getElementById("password");
const logInBtn = document.getElementById("login-button");
const log = document.getElementById("log");
const ready = document.getElementById("ready");
const container = document.querySelector(".container");
date.innerHTML = new Date().getFullYear();
// Define the quiz questions and answers
let quizData = [
{
question: "What is the capital of Nigeria?",
options: ["Abuja", "Lagos", "Aso rock", "Makurdi"],
answer: 1,
correctAnswer: false
},
{
question: "Who is the first president of Nigeria?",
options: ["Bola Ahmed Tinubu","Dr. Nnamdi Azikiwe", "Gen. Muhammadu Buhari", "Alh. Shehu Shagari"],
answer: 2,
correctAnswer: false
},
{
question: "How many states are in Nigeria?",
options: ["36", "50", "37", "30"],
answer: 1,
correctAnswer: false
},
{
question: "Where is the Eiffel tower located?",
options: ["Ontario", "London", "Paris", "Abeokuta"],
answer: 3,
correctAnswer: false
},
{
question: "Which of this is a coastal city?",
options: ["Enugu", "Owerri", "Lagos", "Kebbi"],
answer: 3,
correctAnswer: false
}
];
// Define functions
// Show quiz questions and options
function displayQuestion() {
const currentQuiz = quizData[currentQuestion];
questionEl.innerText = currentQuiz.question;
option1El.innerText = currentQuiz.options[0];
option2El.innerText = currentQuiz.options[1];
option3El.innerText = currentQuiz.options[2];
option4El.innerText = currentQuiz.options[3];
}
// Start the quiz and set up the timer
function startQuiz() {
startBtn.style.display = "none";
timerEl.innerText = "Time remaining: 60 seconds";
displayQuestion();
quiz.style.display = "block";
prevBtn.style.display = "none";
nextBtn.style.display = "inline";
ready.style.display = "none";
container.style.background = "rgba(25,25,25,0.3)";
let timeLeft = 60;
timer = setInterval(function() {
timeLeft--;
if (timeLeft <= 0) {
clearInterval(timer);
endQuiz();
}
timerEl.innerText = "Time remaining: " + timeLeft + " seconds";
}, 1000);
}
// Check the selected answer and update the score
function checkAnswer() {
const selectedOption = document.querySelector('[name="option"]:checked');
if (!selectedOption) {
return;
}
const selectedAnswer = parseInt(selectedOption.value);
if (selectedAnswer === quizData[currentQuestion].answer) {
quizData[currentQuestion].correctAnswer = true;
} else {
quizData[currentQuestion].correctAnswer = false;
}
}
// function checkPre() {
// const preOption = document.querySelector('[name="previous-option"]:checked');
// // if (!preOption) {
// // return;
// // }
// const preAnswer = parseInt(preOption.value);
// if (preAnswer === quizData[previousQuestion].answer) {
// alert("pre");
// score--;
// return;
// }
// }
//Calculate score by checking how many of the answers are correct
function calculateScore() {
quizData.forEach((question) => {
if (question.correctAnswer === true) score++;
});
}
// End the quiz and show the result
function endQuiz() {
calculateScore();
clearInterval(timer);
quiz.style.display = "none";
resultEl.style.display = "inline";
for (let i = 0; i < users.length; i++) {
scoreEl.innerText = `${score} out of ${quizData.length}`;
return;
}
}
// Load the next quiz question
function loadNextQuestion() {
prevBtn.style.display = "inline";
checkAnswer();
currentQuestion++;
if (currentQuestion === quizData.length - 1) {
nextBtn.style.display = "none";
submitBtn.style.display = "inline";
}
if (currentQuestion === quizData.length) {
endQuiz();
return;
}
displayQuestion();
// let previousQuestion = currentQuestion;
}
// Load the previous quiz question
function loadPreviousQuestion() {
// checkPre();
currentQuestion--;
// score--;
if (currentQuestion === 0) {
prevBtn.style.display = "none";
// submitBtn.style.display = "inline";
}
displayQuestion();
}
// Handle the submit button click event
function submitQuiz() {
checkAnswer();
endQuiz();
}
// // Event listeners
// logInBtn.addEventListener("click",logIn);
startBtn.addEventListener("click",startQuiz);
startBtn.addEventListener("click", function(event) {
options.classList.remove("hidden")
});
nextBtn.addEventListener("click", loadNextQuestion);
prevBtn.addEventListener("click", loadPreviousQuestion);
prevBtn.addEventListener("click", function(event) {
nextBtn.style.display = "inline"
});
submitBtn.addEventListener("click", submitQuiz);
// function resetState() {
// clearStatusClass(document.body);
// nextBtn.classList.add('hide');
// }