-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
183 lines (154 loc) · 5.44 KB
/
test.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
(
function()
{
const StartMin = 5;
let time = StartMin*60;
const countdown = document.getElementById("count");
setInterval(updateCount,1000);
function updateCount()
{
const minutes =Math.floor(time/60);
let second = time % 60;
second = second<10 ? '0'+second : second;
if(time<60)
{
countdown.style.color = 'red';
}
countdown.innerHTML = `${minutes} : ${second}`
time--;
}
var questions = [{
question: "Which of the following phenomenon cannot be observed on the surface of the moon",
choices: ["Rising and setting of the sun","Solar eclipse","Motion of comets","Twinking of Star"],
correctAnswer: 3
}, {
question: "which of the following was the first artificial satellite",
choices: ["Lander Beagle","Sojourner","Apollo 11","Sputnik"],
correctAnswer: 3
}, {
question: "Black hole is",
choices: ["A dark hollow cavity","A massive collapsing star","The other side of sun","Neutron Star"],
correctAnswer: 1
}, {
question: "The first person to enter into space was",
choices: ["Valentina Tereshkova","Edward H.White","Yuri Gagarin","Alan Shepard"],
correctAnswer: 2
}, {
question: "Which of the following planets is named after Greek God,Unlike others which are named after Roman God",
choices: ["Uranus","Neptune","Jupiter","Mercury"],
correctAnswer: 0
}];
var questionCounter = 0;
var selections = [];
var quiz = $('#quiz');
displayNext();
$('#next').on('click', function (e)
{
e.preventDefault();
if(quiz.is(':animated'))
{
return false;
}
choose();
if (isNaN(selections[questionCounter]))
{
alert('Please make a selection!');
}
else
{
questionCounter++;
displayNext();
}
});
$('#start').on('click', function (e)
{
e.preventDefault();
if(quiz.is(':animated'))
{
return false;
}
questionCounter = 0;
selections = [];
displayNext();
$('#start').hide();
$('#next').show();
});
$('.button').on('mouseenter', function ()
{
$(this).addClass('active');
});
$('.button').on('mouseleave', function ()
{
$(this).removeClass('active');
});
function createQuestionElement(index)
{
var qElement = $('<div>', {
id: 'question'
});
var header = $('<h2>Question ' + (index + 1) + ':</h2>');
qElement.append(header);
var question = $('<p>').append(questions[index].question);
qElement.append(question);
var radioButtons = createRadios(index);
qElement.append(radioButtons);
return qElement;
}
function createRadios(index)
{
var radioList = $('<ul>');
var item;
var input = '';
for (var i = 0; i < questions[index].choices.length; i++)
{
item = $('<li>');
input = '<input type="radio" name="answer" value=' + i + ' />';
input += questions[index].choices[i];
item.append(input);
radioList.append(item);
}
return radioList;
}
function choose()
{
selections[questionCounter] = +$('input[name="answer"]:checked').val();
}
function displayNext()
{
quiz.fadeOut(function()
{
$('#question').remove();
if(questionCounter < questions.length)
{
var nextQuestion = createQuestionElement(questionCounter);
quiz.append(nextQuestion).fadeIn();
if (!(isNaN(selections[questionCounter])))
{
$('input[value='+selections[questionCounter]+']').prop('checked', true);
}
}
else
{
var scoreElem = displayScore();
quiz.append(scoreElem).fadeIn();
$('#next').hide();
$('#start').show();
}
});
}
function displayScore()
{
var score = $('<p>',{id: 'question'});
var numCorrect = 0;
for (var i = 0; i < selections.length; i++)
{
if (selections[i] === questions[i].correctAnswer)
{
numCorrect++;
}
}
score.append('You got ' + numCorrect + ' questions out of ' +
questions.length + ' Correct!!!');
return score;
}
})();