-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
212 lines (173 loc) · 5.62 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
'use strict'
const questions = [
{
question: 'HTML Document can contain ______________.',
answer: ['Attributes', 'Tags', 'Plain Text', 'All of These'],
correct: '3'
},
{
question: 'Page Designed in HTML is called as ________.',
answer: ['Server Page', 'Web Page', 'Front Page', 'Yellow Page'],
correct: '1'
},
{
question: 'HTML program is saved using ____________ extension.',
answer: ['.htnl', '.htl', '.hml', '.html'],
correct: '3'
},
{
question: 'HTML program can be read and rendered by _________.',
answer: ['Web Browser', 'Interpreter', 'Server', 'Compiler'],
correct: '0'
},
{
question: 'Which of the following is not an example of browser ?',
answer: ['Microsoft Bing', 'Opera', 'Mozilla', 'Chrome'],
correct: '0'
},
{
question: 'HTML was firstly proposed in year _______.',
answer: ['1990', '1980', '1995', '2000'],
correct: '0'
},
{
question: 'HTML document contain 1 root tag called __________.',
answer: ['HTML', 'BODY', 'HEAD', 'TITLE'],
correct: '0'
}
];
let STORE = {
correct : 0,
incorrect : 0,
questionno : 0,
useranswers : new Array(questions.length),
msg : ""
};
function startQuiz() {
$('.start').hide();
$('.quiz').show();
$('.final').hide();
renderQuestion();
renderScoreBoard();
}
function renderScoreBoard() {
$('#questionnumber').text("Question number: " + (STORE.questionno + 1) + " out of " + questions.length);
$('#score').text(" Correct: " + STORE.correct + " / Incorrect: " + STORE.incorrect);
}
function renderQuestion() {
$('#lblmsg').html("");
document.getElementById("question").innerHTML = loadQuestion();
$('#btnSubmit').show();
$('#btnNext').hide();
$('#btnFinish').hide();
}
function loadQuestion() {
let formMaker = "";
formMaker += "<form><fieldset>";
formMaker += "<label>" + questions[STORE.questionno].question + "</label><br/><br/>";
for (let i = 0; i < 4; i++) {
formMaker += "<input type='radio' id='cbxa" + i + "' class='radio' name='answers' value=" + i + " />";
formMaker += "<label id='cbxa" + i + "' for='cbxa" + i + "'>" + questions[STORE.questionno].answer[i] + "</label><br/>";
}
formMaker += "</fieldset></form>";
return formMaker;
}
function checkUserAns() {
let value = -1;
let correct;
$("input:radio[name='answers']:checked").each(function () {
value = $(this).attr("value");
STORE.useranswers[STORE.questionno] = value;
correct = (value == questions[STORE.questionno].correct) ? true : false;
});
return correct;
}
function updateScore() {
if (STORE.useranswers[STORE.questionno] == questions[STORE.questionno].correct) {
STORE.correct += 1;
}
else {
STORE.incorrect += 1;
}
}
function renderFinish() {
let listmaker = "<h4>Study Section</h4><ul>";
for (let i = 0; i < STORE.useranswers.length; i++) {
if (STORE.useranswers[i] != questions[i].correct) {
listmaker += "<li>" + questions[i].question + "<br/>" + questions[i].answer[questions[i].correct] + "</li>";
}
}
listmaker += "</ul>";
return listmaker;
}
function start(){
STORE.correct = 0;
STORE.incorrect = 0;
STORE.questionno = 0;
STORE.useranswers = new Array(questions.length);
STORE.msg = "";
$('.start').show();
$('.quiz').hide();
$('.final').hide();
}
function submitAns() {
if (checkUserAns() == undefined) {
STORE.msg = "Please Select an Answer";
$('#lblmsg').html(STORE.msg)
$('#lblmsg').css('color', 'red');
}
else {
if (checkUserAns() == true) {
STORE.msg = "Well Done!";
$('#lblmsg').html(STORE.msg)
$('#lblmsg').css('color', 'green');
}
else {
STORE.msg = "Sorry the Correct Answer is :" + questions[STORE.questionno].answer[questions[STORE.questionno].correct];
$('#lblmsg').html(STORE.msg)
$('#lblmsg').css('color', 'red');
}
$(".radio").attr('disabled', true);
updateScore();
renderScoreBoard();
STORE.questionno += 1;
if (STORE.questionno < questions.length) {
$('#btnSubmit').hide();
$('#btnNext').show();
$('#btnFinish').hide();
}
else {
$('#btnSubmit').hide();
$('#btnNext').hide();
$('#btnFinish').show();
}
}
}
function nextQuestion() {
renderQuestion();
}
function finishQuiz() {
let score = ((STORE.correct / questions.length) * 100).toFixed(0);
if (score >= 75) {
//pass
$('#lblMessageHeader').text("Congratulations!");
$('#lblMessageContent').text("You have passed the Quiz");
$('#lblScore').text("Your score is: " + score + "/100");
}
else {
//fail
$('#lblMessageHeader').text("Try Again!");
$('#lblMessageContent').text("You have failed the Quiz. The following are the correct answers.");
$('#lblScore').text("Your score is: " + score + "/100");
}
document.getElementById("lblCorrectAns").innerHTML = renderFinish();
$('.start').hide();
$('.quiz').hide();
$('.final').show();
}
$('#btnSubmit').click(function (event) { submitAns(); });
$('#btnNext').click(function (event) { nextQuestion(); });
$('#btnFinish').click(function (event) { finishQuiz(); });
$('#btnStart').click(function (event) { startQuiz(); });
$('#btnRestart').click(function (event) { start(); });
$(start);