-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
248 lines (205 loc) · 8.08 KB
/
Copy pathscript.js
File metadata and controls
248 lines (205 loc) · 8.08 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
236
237
238
239
240
241
242
243
244
245
246
247
248
const questions = [
{
question: "What is the primary greenhouse gas responsible for climate change?",
answers: [
{prefix: "A", text: "Oxygen", correct: false},
{prefix: "B", text: "Carbon Dioxide", correct: true},
{prefix: "C", text: "Nitrogen", correct: false},
{prefix: "D", text: "Methane", correct: false},
]
},
{
question: "What does SDG stand for in the context of global goals?",
answers: [
{prefix: "A", text: "Sustainable Development Goals", correct: true},
{prefix: "B", text: "Social Development Goals", correct: false},
{prefix: "C", text: "Scientific Development Goals", correct: false},
{prefix: "D", text: "Sustainable Development Grants", correct: false},
]
},
{
question: "What is the main purpose of the United Nations Sustainable Development Goals (SDGs)?",
answers: [
{prefix: "A", text: "To promote global trade", correct: false},
{prefix: "B", text: "To end poverty and protect the planet", correct: true},
{prefix: "C", text: "To establish international military alliances", correct: false},
{prefix: "D", text: "To increase global energy consumption", correct: false},
]
},
{
question: "Which of the following is a renewable energy source?",
answers: [
{prefix: "A", text: "Coal", correct: false},
{prefix: "B", text: "Natural Gas", correct: false},
{prefix: "C", text: "Solar Power", correct: true},
{prefix: "D", text: "Oil", correct: false},
]
},
{
question: "Which global organization is responsible for coordinating international efforts to combat climate change?",
answers: [
{prefix: "A", text: "World Health Organization (WHO)", correct: false},
{prefix: "B", text: "International Monetary Fund (IMF)", correct: false},
{prefix: "C", text: "United Nations Framework Convention on Climate Change (UNFCCC)", correct: true},
{prefix: "D", text: "World Trade Organization (WTO)", correct: false},
]
},
{
question: "Which practice is essential for reducing plastic pollution?",
answers: [
{prefix: "A", text: "Using single-use plastic bags", correct: false},
{prefix: "B", text: "Recycling plastic waste", correct: true},
{prefix: "C", text: "Disposing of plastic in landfills", correct: false},
{prefix: "D", text: "Burning plastic waste", correct: false},
]
}
];
const scoreELement = document.getElementById("score");
const quizArea = document.getElementById("quiz-area");
const answerButtons = document.getElementById("answers-area");
const nextButton = document.getElementById("submit-button");
const bulletsSpanContainer = document.querySelector(".bullets .spans");
let currentQuestionIndex = 0;
let score = 0;
let winScore = 3;
function startQuiz() {
score = 0;
currentQuestionIndex = 0;
score = 0;
createBullets(questions.length);
showQuestion();
}
function createBullets(num) {
// Reset
bulletsSpanContainer.innerHTML = ""
// Create Spans
for (let i = 0; i < num; i++) {
// Create Bullet
let theBullet = document.createElement("span");
// Check If Its First Span
if (i === 0) {
theBullet.className = "on";
}
// Append Bullets To Main Bullet Container
bulletsSpanContainer.appendChild(theBullet);
}
}
function resetState() {
scoreELement.innerHTML = score;
nextButton.innerHTML = "Next";
quizArea.innerHTML = ""
nextButton.style.display = "none";
while(answerButtons.firstChild) {
answerButtons.removeChild(answerButtons.firstChild);
}
}
function showQuestion() {
resetState();
let currentQuestion = questions[currentQuestionIndex]
let questionNo = currentQuestionIndex + 1;
questionTitle(questionNo, currentQuestion);
listQuestions(currentQuestion);
handleBullets();
}
function handleBullets() {
let bulletsSpans = document.querySelectorAll(".bullets .spans span");
let arrayOfSpans = Array.from(bulletsSpans);
for (const [i, span] of arrayOfSpans.entries()) {
if (currentQuestionIndex === i) {
span.className = "on";
}
}
}
function handleAnsweredBullets(isCorrect) {
console.log(currentQuestionIndex, isCorrect)
let bulletsSpans = document.querySelectorAll(".bullets .spans span");
let arrayOfSpans = Array.from(bulletsSpans);
for (const [i, span] of arrayOfSpans.entries()) {
if (currentQuestionIndex === i) {
isCorrect ? span.className = "correct" : span.className = "wrong"
}
}
}
function questionTitle(questionNo, currentQuestion) {
// Create H2 Question Title
let questionTitle = document.createElement("h2");
// Create Question Text
let questionText = document.createTextNode(`${questionNo}. ${currentQuestion.question}`);
// Append Text To H2
questionTitle.appendChild(questionText);
// Append The H2 To The Quiz Area
quizArea.appendChild(questionTitle);
}
function listQuestions(currentQuestion) {
const answers = currentQuestion.answers;
for (const answer of answers) {
const divQuestion = document.createElement("div");
// Create Prefix Question
const prefix = document.createElement("p");
const button = document.createElement("button");
prefix.innerHTML = answer.prefix;
button.innerHTML = answer.text;
divQuestion.classList.add("choice-container");
prefix.classList.add("choice-prefix");
button.classList.add("answer-button");
divQuestion.appendChild(prefix);
divQuestion.appendChild(button);
answerButtons.appendChild(divQuestion);
if(answer.correct){
button.dataset.correct = answer.correct;
}
button.addEventListener("click", selectAnswer)
}
}
function selectAnswer(e) {
const prefixBtn = e.target.parentNode.children[0];
const selectedBtn = e.target;
const isCorrect = selectedBtn.dataset.correct;
let gotItRight = false;
if(isCorrect) {
prefixBtn.classList.add("correct")
selectedBtn.classList.add("correct");
gotItRight = true;
score++;
scoreELement.innerHTML = score;
handleAnsweredBullets(true)
} else {
prefixBtn.classList.add("incorrect")
selectedBtn.classList.add("incorrect");
handleAnsweredBullets(false)
}
const answerButtonChildren = Array.from(answerButtons.children);
for (const answerButtonChild of answerButtonChildren) {
const prefix = answerButtonChild.children[0]
const button = answerButtonChild.children[1];
if(button.dataset.correct && !gotItRight) {
button.classList.add("answered-wrong-here-correct")
prefix.classList.add("answered-wrong-here-correct")
}
button.disabled = true;
}
nextButton.style.display = "block";
}
function showFinalScore() {
resetState();
finalScore = `<div class="text-center mb-3"> <h1 class="h1 ${winScore <= score ? "text-success" : "text-danger"}">${winScore <= score ? "Passed." : "Failed."}</h1> <h1 class="h1 text-primary">${score}</h1> <h3 class="h3 text-primary">out of ${questions.length}</h3> </div>`
quizArea.innerHTML = finalScore;
nextButton.innerHTML = "Play Again";
nextButton.style.display = "block";
}
function handleNextButton() {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
} else {
showFinalScore();
}
}
nextButton.addEventListener("click", () => {
if(currentQuestionIndex < questions.length) {
handleNextButton();
} else {
startQuiz();
}
})
startQuiz();