-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
171 lines (143 loc) · 4.93 KB
/
Copy pathscript.js
File metadata and controls
171 lines (143 loc) · 4.93 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
// 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 questionText = document.getElementById("question-text");
const answersContainer = document.getElementById("answers-container");
const currentQuestionSpan = document.getElementById("current-question");
const totalQuestionsSpan = 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 restartButton = document.getElementById("restart-btn");
const progressBar = document.getElementById("progress");
const quizQuestions = [
{
question: "What is the capital of France?",
answers: [
{ 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?",
answers: [
{ text: "Venus", correct: false },
{ text: "Mars", correct: true },
{ text: "Jupiter", correct: false },
{ text: "Saturn", correct: false },
],
},
{
question: "What is the largest ocean on Earth?",
answers: [
{ text: "Atlantic Ocean", correct: false },
{ text: "Indian Ocean", correct: false },
{ text: "Arctic Ocean", correct: false },
{ text: "Pacific Ocean", correct: true },
],
},
{
question: "Which of these is NOT a programming language?",
answers: [
{ text: "Java", correct: false },
{ text: "Python", correct: false },
{ text: "Banana", correct: true },
{ text: "JavaScript", correct: false },
],
},
{
question: "What is the chemical symbol for gold?",
answers: [
{ text: "Go", correct: false },
{ text: "Gd", correct: false },
{ text: "Au", correct: true },
{ text: "Ag", correct: false },
],
},
];
//QUIZ STATS VARS
let currentQuestionIndex = 0;
let score = 0
let answerDisabled = false;
totalQuestionsSpan.textContent = quizQuestions.length;
maxScoreSpan.textContent = quizQuestions.length;
startButton.addEventListener("click", startQuiz);
restartButton.addEventListener("click", restartQuiz);
function startQuiz(){
console.log("quiz started");
currentQuestionIndex = 0;
score=0;
scoreSpan.textContent = 0;
startScreen.classList.remove("Active");
quizScreen.classList.add("Active");
showQuestion();
}
function showQuestion(){
answerDisabled = false;
const currentQuestion = quizQuestions[currentQuestionIndex];
currentQuestionSpan.textContent = currentQuestionIndex + 1;
const progressPercent = (currentQuestionIndex / quizQuestions.length) * 100;
progressBar.style.width = progressPercent + "%";
questionText.textContent = currentQuestion.question;
answersContainer.innerHTML = "";
currentQuestion.answers.forEach((answer) =>{
const button = document.createElement("button");
button.textContent = answer.text;
button.classList.add("answer-btn");
button.dataset.correct = answer.correct
button.addEventListener("click", selectAnswer)
answersContainer.appendChild(button);
});
}
function selectAnswer(event)
{
if(answerDisabled) return;
answerDisabled = true;
const selectedButton = event.target;
const isCorrect = selectedButton.dataset.correct === "true";
Array.from(answersContainer.children).forEach((button) =>{
if (button.dataset.correct === "true") {
button.classList.add("correct");
}else if(button === selectedButton){
button.classList.add("incorrect");
}
});
if(isCorrect){
score++;
scoreSpan.textContent = score;
}
setTimeout(()=>{
currentQuestionIndex++;
if (currentQuestionIndex < quizQuestions.length) {
showQuestion();
}else{
showResult();
}
},1000);
}
function showResult(){
quizScreen.classList.remove("Active");
resultScreen.classList.add("Active");
finalScoreSpan.textContent = score;
const percentage = (score/quizQuestions.length)*100;
if (percentage === 100) {
resultMessage.textContent = "Perfect! You're a genius!";
} else if (percentage >= 80) {
resultMessage.textContent = "Great job! You know your stuff!";
} else if (percentage >= 60) {
resultMessage.textContent = "Good effort! Keep learning!";
} else if (percentage >= 40) {
resultMessage.textContent = "Not bad! Try again to improve!";
} else {
resultMessage.textContent = "Keep studying! You'll get better!";
}
}
function restartQuiz(){
resultScreen.classList.remove("Active");
startQuiz();
}