|
@@ -0,0 +1,299 @@
|
|
1
|
+//
|
|
2
|
+// lib/lib.js
|
|
3
|
+//
|
|
4
|
+var Question = function (questionObj) {
|
|
5
|
+ this.value = {
|
|
6
|
+ text: "Question",
|
|
7
|
+ answers: []
|
|
8
|
+ };
|
|
9
|
+
|
|
10
|
+ this.selectedAnswer = null;
|
|
11
|
+ this.html = null;
|
|
12
|
+ this.questionText = null;
|
|
13
|
+ this.questionAnswers = null;
|
|
14
|
+ this.questionFeedback = null;
|
|
15
|
+
|
|
16
|
+ this.value = Object.assign(this.value, questionObj);
|
|
17
|
+
|
|
18
|
+ this.onQuestionAnswered = ({ detail }) => {
|
|
19
|
+ this.selectedAnswer = {
|
|
20
|
+ value: detail.answer,
|
|
21
|
+ html: detail.answerHtml
|
|
22
|
+ };
|
|
23
|
+ this.update();
|
|
24
|
+
|
|
25
|
+ document.dispatchEvent(
|
|
26
|
+ new CustomEvent("question-answered", {
|
|
27
|
+ detail: {
|
|
28
|
+ question: this,
|
|
29
|
+ answer: detail.answer
|
|
30
|
+ }
|
|
31
|
+ })
|
|
32
|
+ );
|
|
33
|
+ };
|
|
34
|
+
|
|
35
|
+ this.create = function () {
|
|
36
|
+ this.html = document.createElement("div");
|
|
37
|
+ this.html.classList.add("question");
|
|
38
|
+
|
|
39
|
+ this.questionText = document.createElement("h2");
|
|
40
|
+ this.questionText.textContent = this.value.text;
|
|
41
|
+
|
|
42
|
+ this.questionAnswers = document.createElement("div");
|
|
43
|
+ this.questionAnswers.classList.add("answers");
|
|
44
|
+
|
|
45
|
+ for (let i = 0; i < this.value.answers.length; i++) {
|
|
46
|
+ const ansObj = this.value.answers[i];
|
|
47
|
+ let answer = createAnswer(ansObj);
|
|
48
|
+
|
|
49
|
+ answer.onclick = (ev) => {
|
|
50
|
+ if (this.selectedAnswer !== null) {
|
|
51
|
+ this.selectedAnswer.html.classList.remove("selected");
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ answer.classList.add("selected");
|
|
55
|
+
|
|
56
|
+ this.html.dispatchEvent(
|
|
57
|
+ new CustomEvent("question-answered", {
|
|
58
|
+ detail: {
|
|
59
|
+ answer: ansObj,
|
|
60
|
+ answerHtml: answer
|
|
61
|
+ }
|
|
62
|
+ })
|
|
63
|
+ );
|
|
64
|
+ };
|
|
65
|
+
|
|
66
|
+ this.questionAnswers.appendChild(answer);
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ this.questionFeedback = document.createElement("div");
|
|
70
|
+ this.questionFeedback.classList.add("question-feedback");
|
|
71
|
+
|
|
72
|
+ this.html.appendChild(this.questionText);
|
|
73
|
+ this.html.appendChild(this.questionAnswers);
|
|
74
|
+ this.html.appendChild(this.questionFeedback);
|
|
75
|
+
|
|
76
|
+ this.html.addEventListener("question-answered", this.onQuestionAnswered);
|
|
77
|
+
|
|
78
|
+ return this.html;
|
|
79
|
+ };
|
|
80
|
+
|
|
81
|
+ this.disable = function () {
|
|
82
|
+ this.html.classList.add("disabled");
|
|
83
|
+ this.html.onclick = (ev) => {
|
|
84
|
+ ev.stopPropagation();
|
|
85
|
+ };
|
|
86
|
+
|
|
87
|
+ this.html.removeEventListener("question-answered", this.onQuestionAnswered);
|
|
88
|
+
|
|
89
|
+ let answers = this.html.querySelectorAll(".answer");
|
|
90
|
+ for (let i = 0; i < answers.length; i++) {
|
|
91
|
+ let answer = answers[i];
|
|
92
|
+ answer.onclick = null;
|
|
93
|
+ }
|
|
94
|
+ };
|
|
95
|
+
|
|
96
|
+ this.remove = function () {
|
|
97
|
+ let children = this.html.querySelectorAll("*");
|
|
98
|
+ for (let i = 0; i < children.length; i++) {
|
|
99
|
+ const child = children[i];
|
|
100
|
+ this.html.removeChild(child);
|
|
101
|
+ }
|
|
102
|
+
|
|
103
|
+ this.html.removeEventListener("question-answered", this.onQuestionAnswered);
|
|
104
|
+
|
|
105
|
+ this.html.parentNode.removeChild(this.html);
|
|
106
|
+ this.html = null;
|
|
107
|
+ };
|
|
108
|
+
|
|
109
|
+ this.update = function () {
|
|
110
|
+ let correctFeedback, incorrectFeedback;
|
|
111
|
+ this.html = this.html || document.createElement("div");
|
|
112
|
+
|
|
113
|
+ correctFeedback = "Nice! You got it right.";
|
|
114
|
+ incorrectFeedback = "Oh! Not the correct answer.";
|
|
115
|
+
|
|
116
|
+ if (this.selectedAnswer !== null) {
|
|
117
|
+ if (this.selectedAnswer.value.isCorrect) {
|
|
118
|
+ this.html.classList.add("correct");
|
|
119
|
+ this.html.classList.remove("incorrect");
|
|
120
|
+ this.questionFeedback.innerHTML = correctFeedback;
|
|
121
|
+ } else {
|
|
122
|
+ this.html.classList.add("incorrect");
|
|
123
|
+ this.html.classList.remove("correct");
|
|
124
|
+ this.questionFeedback.innerHTML = incorrectFeedback;
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+ };
|
|
128
|
+
|
|
129
|
+ function createAnswer(obj) {
|
|
130
|
+ this.value = {
|
|
131
|
+ text: "Answer",
|
|
132
|
+ isCorrect: false
|
|
133
|
+ };
|
|
134
|
+
|
|
135
|
+ this.value = Object.assign(this.value, obj);
|
|
136
|
+
|
|
137
|
+ this.html = document.createElement("button");
|
|
138
|
+ this.html.classList.add("answer");
|
|
139
|
+
|
|
140
|
+ this.html.textContent = this.value.text;
|
|
141
|
+
|
|
142
|
+ return this.html;
|
|
143
|
+ }
|
|
144
|
+ };
|
|
145
|
+
|
|
146
|
+ //
|
|
147
|
+ // main.js
|
|
148
|
+ //
|
|
149
|
+
|
|
150
|
+ let questionsData = [
|
|
151
|
+ {
|
|
152
|
+ text: "Channel name",
|
|
153
|
+ answers: [
|
|
154
|
+ {
|
|
155
|
+ text: "Pewdipie",
|
|
156
|
+ isCorrect: false
|
|
157
|
+ },
|
|
158
|
+ {
|
|
159
|
+ text: "SmfCoder",
|
|
160
|
+ isCorrect: true
|
|
161
|
+ },
|
|
162
|
+ {
|
|
163
|
+ text: "Jack",
|
|
164
|
+ isCorrect: false
|
|
165
|
+ }
|
|
166
|
+ ]
|
|
167
|
+ },
|
|
168
|
+ {
|
|
169
|
+ text: "Channel goal",
|
|
170
|
+ answers: [
|
|
171
|
+ {
|
|
172
|
+ text: "Help you become a better developer",
|
|
173
|
+ isCorrect: true
|
|
174
|
+ },
|
|
175
|
+ {
|
|
176
|
+ text: "Help you cook amazing cookies",
|
|
177
|
+ isCorrect: false
|
|
178
|
+ },
|
|
179
|
+ {
|
|
180
|
+ text: "Becoming an artist",
|
|
181
|
+ isCorrect: false
|
|
182
|
+ },
|
|
183
|
+ {
|
|
184
|
+ text: "Don't know",
|
|
185
|
+ isCorrect: false
|
|
186
|
+ }
|
|
187
|
+ ]
|
|
188
|
+ },
|
|
189
|
+ {
|
|
190
|
+ text: "Subscribe and ...",
|
|
191
|
+ answers: [
|
|
192
|
+ {
|
|
193
|
+ text: "Watch all my videos",
|
|
194
|
+ isCorrect: false
|
|
195
|
+ },
|
|
196
|
+ {
|
|
197
|
+ text: 'Comment "I subscribed"',
|
|
198
|
+ isCorrect: false
|
|
199
|
+ },
|
|
200
|
+ {
|
|
201
|
+ text: "Active the notifications",
|
|
202
|
+ isCorrect: true
|
|
203
|
+ }
|
|
204
|
+ ]
|
|
205
|
+ },
|
|
206
|
+ {
|
|
207
|
+ text: "In witch year was the first version of c++ released",
|
|
208
|
+ answers: [
|
|
209
|
+ {
|
|
210
|
+ text: "1982",
|
|
211
|
+ isCorrect: true
|
|
212
|
+ },
|
|
213
|
+ {
|
|
214
|
+ text: "1995",
|
|
215
|
+ isCorrect: false
|
|
216
|
+ },
|
|
217
|
+ {
|
|
218
|
+ text: "1983",
|
|
219
|
+ isCorrect: false
|
|
220
|
+ },
|
|
221
|
+ {
|
|
222
|
+ text: "1985",
|
|
223
|
+ isCorrect: false
|
|
224
|
+ }
|
|
225
|
+ ]
|
|
226
|
+ },
|
|
227
|
+ {
|
|
228
|
+ text: "What is France capital",
|
|
229
|
+ answers: [
|
|
230
|
+ {
|
|
231
|
+ text: "Lyon",
|
|
232
|
+ isCorrect: true
|
|
233
|
+ },
|
|
234
|
+ {
|
|
235
|
+ text: "Marseille",
|
|
236
|
+ isCorrect: false
|
|
237
|
+ },
|
|
238
|
+ {
|
|
239
|
+ text: "Paris",
|
|
240
|
+ isCorrect: true
|
|
241
|
+ },
|
|
242
|
+ {
|
|
243
|
+ text: "Lille",
|
|
244
|
+ isCorrect: false
|
|
245
|
+ }
|
|
246
|
+ ]
|
|
247
|
+ }
|
|
248
|
+ ];
|
|
249
|
+
|
|
250
|
+ // variables initialization
|
|
251
|
+ let questions = [];
|
|
252
|
+ let score = 0,
|
|
253
|
+ answeredQuestions = 0;
|
|
254
|
+ let appContainer = document.getElementById("questions-container");
|
|
255
|
+ let scoreContainer = document.getElementById("score-container");
|
|
256
|
+ scoreContainer.innerHTML = `Score: ${score}/${questionsData.length}`;
|
|
257
|
+
|
|
258
|
+ /**
|
|
259
|
+ * Shuffles array in place. ES6 version
|
|
260
|
+ * @param {Array} arr items An array containing the items.
|
|
261
|
+ */
|
|
262
|
+ function shuffle(arr) {
|
|
263
|
+ for (let i = arr.length - 1; i > 0; i--) {
|
|
264
|
+ const j = Math.floor(Math.random() * (i + 1));
|
|
265
|
+ [arr[i], arr[j]] = [arr[j], arr[i]];
|
|
266
|
+ }
|
|
267
|
+ }
|
|
268
|
+
|
|
269
|
+ shuffle(questionsData);
|
|
270
|
+
|
|
271
|
+ // creating questions
|
|
272
|
+ for (var i = 0; i < questionsData.length; i++) {
|
|
273
|
+ let question = new Question({
|
|
274
|
+ text: questionsData[i].text,
|
|
275
|
+ answers: questionsData[i].answers
|
|
276
|
+ });
|
|
277
|
+
|
|
278
|
+ appContainer.appendChild(question.create());
|
|
279
|
+ questions.push(question);
|
|
280
|
+ }
|
|
281
|
+
|
|
282
|
+ document.addEventListener("question-answered", ({ detail }) => {
|
|
283
|
+ if (detail.answer.isCorrect) {
|
|
284
|
+ score++;
|
|
285
|
+ }
|
|
286
|
+
|
|
287
|
+ answeredQuestions++;
|
|
288
|
+ scoreContainer.innerHTML = `Score: ${score}/${questions.length}`;
|
|
289
|
+ detail.question.disable();
|
|
290
|
+
|
|
291
|
+ if (answeredQuestions == questions.length) {
|
|
292
|
+ setTimeout(function () {
|
|
293
|
+ alert(`Quiz completed! \nFinal score: ${score}/${questions.length}`);
|
|
294
|
+ }, 100);
|
|
295
|
+ }
|
|
296
|
+ });
|
|
297
|
+
|
|
298
|
+ console.log(questions, questionsData);
|
|
299
|
+
|