-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
527 lines (438 loc) · 13.3 KB
/
script.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
class Question {
constructor(question, answers, correctAns) {
let [_ansA, _ansB, _ansC, _ansD] = [...answers];
this.ansA = _ansA;
this.ansB = _ansB;
this.ansC = _ansC;
this.ansD = _ansD;
this.question = question;
this.correctAns = correctAns;
if (Question.count === undefined) Question.count = 0;
Question.count++;
}
checkAnswer(ans) {
if (this.correctAns === ans) return true;
else return false;
}
showQuestion(num) {
if ("content" in document.createElement("template")) {
const commandLine = document.querySelector("main");
const template = document.querySelector("#questionLine");
const clone = template.content.cloneNode(true);
const header = clone.querySelector(".header");
const question = clone.querySelector(".question");
const ansA = clone.querySelector(".ansA");
const ansB = clone.querySelector(".ansB");
const ansC = clone.querySelector(".ansC");
const ansD = clone.querySelector(".ansD");
const number = parseInt(num) + 1;
header.innerHTML = "question-" + number;
question.innerHTML = this.question;
ansA.innerHTML += this.ansA;
ansB.innerHTML += this.ansB;
ansC.innerHTML += this.ansC;
ansD.innerHTML += this.ansD;
commandLine.append(clone);
} else {
alert("Your browswere doesn't support taplate.");
}
}
showMisstake(num, userAns) {
if ("content" in document.createElement("template")) {
const commandLine = document.querySelector("main");
const template = document.querySelector("#questionLine");
const clone = template.content.cloneNode(true);
const header = clone.querySelector(".header");
const question = clone.querySelector(".question");
const ansA = clone.querySelector(".ansA");
const ansB = clone.querySelector(".ansB");
const ansC = clone.querySelector(".ansC");
const ansD = clone.querySelector(".ansD");
const number = parseInt(num);
header.innerHTML = "question-" + number;
question.innerHTML = this.question;
ansA.innerHTML += this.ansA;
ansB.innerHTML += this.ansB;
ansC.innerHTML += this.ansC;
ansD.innerHTML += this.ansD;
const correctAnsColor = "#03C03C";
const wrongAnscolor = "#9e0000";
switch (this.correctAns) {
case "a":
ansA.style.color = correctAnsColor;
break;
case "b":
ansB.style.color = correctAnsColor;
break;
case "c":
ansC.style.color = correctAnsColor;
break;
case "d":
ansD.style.color = correctAnsColor;
break;
default:
alert("Sorry! We have some problems. We will fix them as soon as possible");
break;
}
switch (userAns) {
case "a":
ansA.style.color = wrongAnscolor;
break;
case "b":
ansB.style.color = wrongAnscolor;
break;
case "c":
ansC.style.color = wrongAnscolor;
break;
case "d":
ansD.style.color = wrongAnscolor;
break;
default:
alert("Sorry! We have some problems. We will fix them as soon as possible");
break;
}
commandLine.append(clone);
} else {
alert("Your browswere doesn't support template.");
}
}
}
function shuffleArray(arr) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}
function showError(lineHeader, content) {
// Create element
const commandLine = document.querySelector("main");
let line = document.createElement("div");
line.classList.add("line", "error");
let span = document.createElement("span");
span.innerText = "programmer-quiz\\";
let header = document.createElement("span");
header.classList.add("header");
header.innerText = lineHeader;
span.appendChild(header);
span.innerHTML += "> ";
line.appendChild(span);
line.innerText += content;
commandLine.appendChild(line);
}
function getAnswer(lineHeader) {
return new Promise((resolve) => {
// Create element
const commandLine = document.querySelector("main");
let line = document.createElement("div");
line.classList.add("line", "has-answer");
let span = document.createElement("span");
span.innerText = "programmer-quiz\\";
let header = document.createElement("span");
header.classList.add("header");
header.innerText = lineHeader;
span.appendChild(header);
span.innerHTML += "> ";
line.appendChild(span);
let answerField = document.createElement("input");
answerField.classList.add("answer");
answerField.setAttribute("type", "text");
line.appendChild(answerField);
commandLine.appendChild(line);
// Set complementing width to answer field
answerField.style.width = line.offsetWidth - span.offsetWidth - 10 + "px";
// Focus div
const answerFields = document.querySelectorAll(".answer");
let lastAnswerField = answerFields[answerFields.length - 1];
lastAnswerField.focus();
// Submit answer
lastAnswerField.addEventListener("keydown", (e) => {
if (e.keyCode == 13) {
if(e.target.value.toLowerCase().trim() === "clear") {
clearCommandLine();
e.target.value = "";
} else {
e.target.setAttribute("readonly", "readonly");
resolve(e.target.value.toLowerCase().trim());
}
}
});
});
}
function validateAnswer(value, avaibleAsnwers) {
return new Promise((resolve, reject) => {
if (Array.isArray(avaibleAsnwers)) {
resolve(avaibleAsnwers.includes(value));
} else {
reject(
"Sorry! We have some problems. We will fix them as soon as possible."
);
}
});
}
async function getUserAnswer(lineHeader, avaibleAsnwers) {
let userEnteredCorrectly = false;
while (!userEnteredCorrectly) {
const userAns = await getAnswer(lineHeader);
if (await validateAnswer(userAns, avaibleAsnwers)) {
userEnteredCorrectly = true;
return new Promise((resolve) => {
resolve(userAns);
});
} else {
showError(lineHeader, "We don't have that answer. Please type again...");
}
}
}
async function startQuiz(userAns) {
if (userAns === "go") {
let score = 0;
let misstakes = [];
for (let q in questions) {
const currentQuestion = questions[q];
const questionNum = parseInt(q) + 1;
await currentQuestion.showQuestion(q);
let userAns = await getUserAnswer(`question-${questionNum}`, [
"a",
"b",
"c",
"d",
]);
if (await currentQuestion.checkAnswer(userAns)) score++;
else misstakes.push({
question: questions[q],
userAnswer: userAns,
questionNum: questionNum,
});
// console.log(misstakes);
}
return new Promise((resolve) => {
resolve({score: score, misstakes: misstakes,});
});
}
}
function showCommonLine(lineHeader, content) {
// Create element
const commandLine = document.querySelector("main");
let line = document.createElement("div");
line.classList.add("line");
let span = document.createElement("span");
span.innerText = "programmer-quiz\\";
let header = document.createElement("span");
header.classList.add("header");
header.innerText = lineHeader;
span.appendChild(header);
span.innerHTML += "> ";
line.appendChild(span);
line.innerHTML += content;
commandLine.appendChild(line);
}
async function showSummary(userScore) {
switch (userScore) {
case 0:
case 1:
case 2:
case 3:
showCommonLine(
"summary",
"It could be better but, your score is " +
userScore +
" points. We think you could use some more learning."
);
break;
case 4:
case 5:
case 6:
case 7:
showCommonLine(
"summary",
"Good, your score is " +
userScore +
" points. So a little study more and you will be a great programmer."
);
break;
case 8:
case 9:
showCommonLine(
"summary",
"Congratulation! Your score is " +
userScore +
" points. A few mistake isn't tragedy. You are a good programmer."
);
break;
case 10:
showCommonLine(
"summary",
"Perfect! Your score is " +
userScore +
" points, that is to say, you're a awesome programmer."
);
break;
default:
showError(
"summary",
"Sorry! We have some problems. We will fix them as soon as possible."
);
break;
}
showCommonLine(
"summary",
'If you want to see your misstakes type <span class="marked">MORE</span>. If you want to repeat type <span class="marked">RESTART</span>...'
);
return await getUserAnswer("summary", ["restart", "more"]);
}
async function repeat() {
const userScore = await startQuiz("go");
const summary = await showSummary(userScore.score);
if (summary === "restart") repeat();
else if (summary === "more") misstakeReview(userScore.misstakes);
}
async function quiz() {
const appStart = await getUserAnswer("start", ["go"]);
const userScore = await startQuiz(appStart);
const summary = await showSummary(userScore.score);
if (summary === "restart") repeat();
else if (summary === "more") misstakeReview(userScore.misstakes);
}
function clearCommandLine() {
const commandLine = document.querySelector("main");
const lines = document.querySelectorAll(".line");
for(const el of lines) {
if(el.classList.contains("error")
|| el.classList.contains("has-answer")
&& el.lastChild.hasAttribute("readonly")) {
el.remove();
}
}
let filteredLines = document.querySelectorAll(".line");
for(let l = 0; l < (filteredLines.length - 2); l++) {
commandLine.removeChild(filteredLines[l]);
}
}
async function misstakeReview(misstakes) {
if(misstakes.length === 0)
showCommonLine("summary", "You didn't make any misstake.")
else
misstakes.forEach(misstake => {
let {question, userAnswer, questionNum} = misstake;
question.showMisstake(questionNum, userAnswer);
});
showCommonLine(
"summary",
'If you want to repeat type <span class="marked">RESTART</span>...'
);
let ans = await getUserAnswer("summary", ["restart"]);
if(ans === "restart")
repeat();
}
window.onload = function () {
let questionBeforeShuffle = [];
// adding questions
questionBeforeShuffle.push(
new Question(
"Which of the following languages is not used for frontend development?",
["HTML", "Java", "CSS", "JavaScript"],
"b"
)
);
questionBeforeShuffle.push(
new Question(
"What does CSS stand for?",
[
"Creative Style System",
"Computer Style Sheets",
"Cascading Style Sheets",
"Colorful Style Symbols",
],
"c"
)
);
questionBeforeShuffle.push(
new Question(
"Which HTML tag is used to define an unordered list?",
["<ul>", "<ol>", "<li>", "<list>"],
"a"
)
);
questionBeforeShuffle.push(
new Question(
"What is the purpose of the JavaScript function `querySelector()`?",
[
"To perform mathematical calculations",
"To add event listeners",
"To change the page URL",
"To select an HTML element by its class or ID",
],
"d"
)
);
questionBeforeShuffle.push(
new Question(
"Which of the following is a valid way to declare a CSS class?",
["#my-class", ".my-class", "class.my-class", "<my-class>"],
"b"
)
);
questionBeforeShuffle.push(
new Question(
"What is the correct syntax for a JavaScript `for` loop?",
[
"for (i = 0; i < 5; i++)",
"for (i < 5; i++)",
"for (i = 0; i++)",
"for (var i = 0; i < 5; i++)",
],
"d"
)
);
questionBeforeShuffle.push(
new Question(
"What does the acronym API stand for in web development?",
[
"Advanced Programming Interface",
"Application Programming Interface",
"Automated Programming Interface",
"Associated Program Interface",
],
"b"
)
);
questionBeforeShuffle.push(
new Question(
"Which of the following is used to make a webpage responsive to different screen sizes?",
["Media queries", "JavaScript", "CSS selectors", "HTML forms"],
"a"
)
);
questionBeforeShuffle.push(
new Question(
"What is the purpose of the HTML <canvas> element?",
[
"To display images",
"To embed videos",
"To create animations and graphics",
"To style text",
],
"c"
)
);
questionBeforeShuffle.push(
new Question(
"What does the CSS property `display: none;` do?",
[
"Increases the font size",
"Changes the background color",
"Hides an element from the webpage",
"Makes an element visible on hover",
],
"c"
)
);
window.questions = shuffleArray(questionBeforeShuffle);
console.log(questions)
showCommonLine(
"start",
`Hi! Do you think you're a good programmer? Let's check this. Type <span class="marked">GO</span> to start the quiz...`
);
quiz();
};