-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
219 lines (157 loc) · 5.3 KB
/
Copy pathscript.js
File metadata and controls
219 lines (157 loc) · 5.3 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
// DOM Elements
const startScreen = document.getElementById("start-screen");
const quizScreen = document.getElementById("quiz-screen");
const resultScreen = document.getElementById("result-screen");
const startButton = document.getElementById("start-btn");
const restartButton = document.getElementById("restart-btn");
const questionText = document.getElementById("question-text");
const answersContainer = document.getElementById("answer-container");
const currentQuestionSpan = document.getElementById("current-question");
const totalQuestionSpan = document.getElementById("total-questions");
const scoreSpan = document.getElementById("score");
const finalScoreSpan = document.getElementById("final-score");
const maxScoreSpan = document.getElementById("max-score");
const resultMessage = document.getElementById("result-message");
const progressBar = document.getElementById("progress");
// Questions
const quizeQuestions = [
{
question: "What is the capital of France?",
answer: [
{ text: "London", correct: false },
{ text: "Berlin", correct: false },
{ text: "Paris", correct: true },
{ text: "Madrid", correct: false }
]
},
{
question: "Which planet is known as the Red Planet?",
answer: [
{ text: "Earth", correct: false },
{ text: "Mars", correct: true },
{ text: "Jupiter", correct: false },
{ text: "Venus", correct: false }
]
},
{
question: "Who developed the Java programming language?",
answer: [
{ text: "James Gosling", correct: true },
{ text: "Dennis Ritchie", correct: false },
{ text: "Bjarne Stroustrup", correct: false },
{ text: "Guido van Rossum", correct: false }
]
},
{
question: "What does HTML stand for?",
answer: [
{ text: "Hyper Text Markup Language", correct: true },
{ text: "High Text Machine Language", correct: false },
{ text: "Hyper Transfer Markup Language", correct: false },
{ text: "Home Tool Markup Language", correct: false }
]
},
{
question: "Which is the largest ocean on Earth?",
answer: [
{ text: "Atlantic Ocean", correct: false },
{ text: "Indian Ocean", correct: false },
{ text: "Arctic Ocean", correct: false },
{ text: "Pacific Ocean", correct: true }
]
}
];
// Quiz State
let currentQuestionIndex = 0;
let score = 0;
let answersDisabled = false;
totalQuestionSpan.textContent = quizeQuestions.length;
maxScoreSpan.textContent = quizeQuestions.length;
// Event Listeners
startButton.addEventListener("click", startQuiz);
restartButton.addEventListener("click", restartQuiz);
// Start Quiz
function startQuiz() {
currentQuestionIndex = 0;
score = 0;
scoreSpan.textContent = score;
startScreen.classList.remove("active");
resultScreen.classList.remove("active");
quizScreen.classList.add("active");
showQuestion();
}
// Show Question
function showQuestion() {
answersDisabled = false;
const currentQuestion = quizeQuestions[currentQuestionIndex];
questionText.textContent = currentQuestion.question;
currentQuestionSpan.textContent = currentQuestionIndex + 1;
answersContainer.innerHTML = "";
currentQuestion.answer.forEach(answer => {
const button = document.createElement("button");
button.innerText = answer.text;
button.classList.add("answer-btn");
button.dataset.correct = answer.correct;
button.addEventListener("click", selectAnswer);
answersContainer.appendChild(button);
});
updateProgressBar();
}
// Select Answer
function selectAnswer(e) {
if (answersDisabled) return;
answersDisabled = true;
const selectedButton = e.target;
const isCorrect =
selectedButton.dataset.correct === "true";
if (isCorrect) {
score++;
scoreSpan.textContent = score;
selectedButton.classList.add("correct");
} else {
selectedButton.classList.add("incorrect");
}
Array.from(answersContainer.children).forEach(button => {
if (button.dataset.correct === "true") {
button.classList.add("correct");
}
button.disabled = true;
});
setTimeout(() => {
currentQuestionIndex++;
if (currentQuestionIndex < quizeQuestions.length) {
showQuestion();
} else {
showResults();
}
}, 1000);
}
// Show Results
function showResults() {
quizScreen.classList.remove("active");
resultScreen.classList.add("active");
finalScoreSpan.textContent = score;
if (score === quizeQuestions.length) {
resultMessage.textContent =
"Excellent! Perfect Score!";
}
else if (score >= 3) {
resultMessage.textContent =
"Good Job! Keep it up!";
}
else {
resultMessage.textContent =
"Keep Practicing!";
}
}
// Restart Quiz
function restartQuiz() {
resultScreen.classList.remove("active");
startQuiz();
}
// Progress Bar
function updateProgressBar() {
const progress =
(currentQuestionIndex / quizeQuestions.length) * 100;
progressBar.style.width = progress + "%";
}