Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
235 lines (169 loc) · 6.11 KB
/
Copy pathscript.js
File metadata and controls
235 lines (169 loc) · 6.11 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* =================================
CODEQUEST - QUIZ SYSTEM
================================= */
// HTML elements
const startBtn = document.getElementById("startBtn");
const restartBtn = document.getElementById("restartBtn");
const welcomeScreen = document.getElementById("welcomeScreen");
const quizScreen = document.getElementById("quizScreen");
const resultScreen = document.getElementById("resultScreen");
const studentName = document.getElementById("studentName");
const subjectSelect = document.getElementById("subject");
const difficultySelect = document.getElementById("difficulty");
const questionTitle = document.getElementById("questionTitle");
const questionContainer = document.getElementById("questionContainer");
const resultText = document.getElementById("resultText");
// Quiz variables
let student = "";
let selectedSubject = "";
let selectedDifficulty = "";
let currentQuestion = 0;
let score = 0;
let quizQuestions = [];
let answered = false;
// Start quiz
startBtn.addEventListener("click", function () {
student = studentName.value.trim();
selectedSubject = subjectSelect.value;
selectedDifficulty = difficultySelect.value;
if (student === "") {
alert("Please enter your name!");
studentName.focus();
return;
}
if (selectedSubject === "") {
alert("Please select a subject!");
subjectSelect.focus();
return;
}
// Get questions from questions.js
if (
typeof questionBank === "undefined" ||
!questionBank[selectedSubject] ||
!questionBank[selectedSubject][selectedDifficulty]
) {
alert("Questions are not available yet for this selection.");
return;
}
quizQuestions = questionBank[selectedSubject][selectedDifficulty];
if (quizQuestions.length === 0) {
alert("No questions available yet!");
return;
}
currentQuestion = 0;
score = 0;
welcomeScreen.classList.add("hidden");
resultScreen.classList.add("hidden");
quizScreen.classList.remove("hidden");
showQuestion();
});
// Show current question
function showQuestion() {
answered = false;
const question = quizQuestions[currentQuestion];
questionTitle.textContent =
"Hello, " + student +
"! Question " + (currentQuestion + 1) +
" of " + quizQuestions.length;
questionContainer.innerHTML = "";
const card = document.createElement("div");
card.className = "quiz-card";
const heading = document.createElement("h3");
heading.textContent = question.question;
card.appendChild(heading);
const progress = document.createElement("p");
progress.className = "message";
progress.textContent =
selectedSubject + " • " +
selectedDifficulty.toUpperCase();
card.appendChild(progress);
// Create answer buttons
question.options.forEach(function (option, index) {
const button = document.createElement("button");
button.className = "option-button";
button.textContent =
String.fromCharCode(65 + index) + ". " + option;
button.addEventListener("click", function () {
checkAnswer(index, button);
});
card.appendChild(button);
});
const feedback = document.createElement("p");
feedback.className = "message";
feedback.id = "answerFeedback";
card.appendChild(feedback);
const nextBtn = document.createElement("button");
nextBtn.id = "nextBtn";
nextBtn.textContent =
currentQuestion === quizQuestions.length - 1
? "Finish Quiz"
: "Next Question →";
nextBtn.disabled = true;
nextBtn.addEventListener("click", function () {
currentQuestion++;
if (currentQuestion < quizQuestions.length) {
showQuestion();
} else {
showResult();
}
});
card.appendChild(nextBtn);
questionContainer.appendChild(card);
}
// Check answer
function checkAnswer(selectedIndex, selectedButton) {
if (answered) {
return;
}
answered = true;
const question = quizQuestions[currentQuestion];
const optionButtons =
questionContainer.querySelectorAll(".option-button");
const feedback = document.getElementById("answerFeedback");
const nextBtn = document.getElementById("nextBtn");
if (selectedIndex === question.answer) {
score++;
feedback.textContent = "Correct answer! 🎉";
feedback.style.color = "#34D399";
selectedButton.classList.add("correct");
} else {
feedback.textContent =
"Correct answer: " + question.options[question.answer];
feedback.style.color = "#FB7185";
selectedButton.classList.add("incorrect");
optionButtons[question.answer].classList.add("correct");
}
optionButtons.forEach(function (button) {
button.disabled = true;
});
nextBtn.disabled = false;
}
// Show final result
function showResult() {
quizScreen.classList.add("hidden");
resultScreen.classList.remove("hidden");
const percentage = Math.round(
(score / quizQuestions.length) * 100
);
let message = "Keep practising! You can improve! 💪";
if (percentage >= 80) {
message = "Excellent work! 🌟";
} else if (percentage >= 50) {
message = "Good effort! Keep learning! 👏";
}
resultText.textContent =
student + ", your score is " +
score + " out of " + quizQuestions.length +
" (" + percentage + "%). " + message;
resultText.className = "score";
}
// Restart quiz
restartBtn.addEventListener("click", function () {
resultScreen.classList.add("hidden");
quizScreen.classList.add("hidden");
welcomeScreen.classList.remove("hidden");
studentName.value = student;
subjectSelect.value = selectedSubject;
difficultySelect.value = selectedDifficulty;
questionContainer.innerHTML = "";
});