-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
144 lines (125 loc) · 3.95 KB
/
quiz.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
(function () {
var allQuestions = [{
question: "'This is a string' instanceof String; What is the result?",
options: ["true", "undefined", "false", "TypeError"],
answer: 2
}, {
question: "10 > 9 > 8 === true; What is the result?",
options: ["true", "NaN", "false", "undefined"],
answer: 2
}, {
question: "NaN === NaN; What is the result?",
options: ["NaN", "false", "undefined", "true"],
answer: 1
}, {
question: "Number('1') - 1 == 0; What is the result?",
options: ["true", "false", "NaN", "undefined"],
answer: 0
}, {
question: "(true + false) > 2 + true; What is the result?",
options: ["NaN", "false", "true", "undefined"],
answer: 1
}, {
question: "'1' - - '1'; What is the result?",
options: ["2", "11", "0", "'11'"],
answer: 0
}, {
question: "[ ] + [ ] + 'Cat'.split(''); What is the result?",
options: ["'C, a ,t'", "['C','a','t']", "[ ][ ]['C','a','t']", "TypeError"],
answer: 0
}, {
question: "new Array(5).toString(); What is the result ?",
options: ["', , , ,'", "[ ]", "'[ ]'", "'['']'"],
answer: 0
}, {
question: "String('Hello') === 'Hello'; What is the result?",
options: ["NaN", "false", "true", "undefined"],
answer: 2
}, {
question: "console.log(typeof typeof 1); What is the result?",
options: ["number", "string", "1", "typeof"],
answer: 1
}];
let questionCounter = 0;
let selectOptions = [];
let quizSpace = $('#quiz');
nextQuestion();
$('#next').click(function () {
chooseOption();
if (isNaN(selectOptions[questionCounter])) {
alert('Ընտրեք որևէ տարբերակ !');
}
else {
questionCounter++;
nextQuestion();
$('#prev').show();
}
});
$('#prev').click(function () {
chooseOption();
questionCounter--;
$('#prev').hide();
nextQuestion();
});
function createElement(index) {
let element = $('<div>', { id: 'question' });
let header = $('<h2>Question ' + (index + 1) + ' .</h2>');
element.append(header);
let question = $('<h4>').append(allQuestions[index].question);
element.append(question);
let radio = radioButtons(index);
element.append(radio);
return element;
}
function radioButtons(index) {
let radioItems = $('<ul col-3-md>');
let item;
let input = '';
for (var i = 0; i < allQuestions[index].options.length; i++) {
item = $('<li>');
input = '<input type="radio" name="answer" value=' + i + ' />';
input += allQuestions[index].options[i];
item.append(input);
radioItems.append(item);
}
return radioItems;
}
function chooseOption() {
selectOptions[questionCounter] = +$('input[name="answer"]:checked').val();
}
function nextQuestion() {
quizSpace.fadeOut(function () {
$('#question').remove();
if (questionCounter < allQuestions.length) {
let nextQuestion = createElement(questionCounter);
quizSpace.append(nextQuestion).fadeIn();
if (!(isNaN(selectOptions[questionCounter]))) {
$('input[value=' + selectOptions[questionCounter] + ']').prop('checked', true);
}
// if (quesCounter === 1) {
// $('#prev').show();
// }
else if (questionCounter == 0) {
$('#prev').hide();
$('#next').show();
}
}
else {
var scoreRslt = displayResult();
quizSpace.append(scoreRslt).fadeIn();
$('#next').hide();
$('#prev').hide();
}
});
}
function displayResult() {
let score = $('<h4>', { id: 'question' });
let correct = 0;
for (var i = 0; i < selectOptions.length; i++) {
if (selectOptions[i] == allQuestions[i].answer) {
correct++;
}
}
return score.append('Your score: ' + correct);
}
})();