Repository navigation
Expand file tree
/
Copy pathquizzer.js
More file actions
222 lines (194 loc) · 4.29 KB
/
Copy pathquizzer.js
File metadata and controls
222 lines (194 loc) · 4.29 KB
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
// 1
var questions = [
[
"¿Que se utiliza para estilizar un sitio web?",
"Javascript",
"CSS",
"PHP",
"AngularJS",
1
],
[
"¿Qué tipo de lenguaje es PHP?",
"Interpretado",
"Compilado",
"Los 2 anteriores",
"Ninguno de los anteriores",
0
],
[
"¿jQuery es una biblioteca para qué lenguaje?",
"Python",
"PHP",
"Java",
"Ninguno de los anteriores",
3
],
[
"¿Cómo se marca el inicio de código PHP?",
"<?php",
"<?",
"Los 2 anteriores",
"Ninguno de los anteriores",
2
],
[
"¿Quién diseño Javascript?",
"Mark Zuckerberg",
"Bill Gates",
"Brendan Eich",
"Rasmus Lerdorf",
2
],
];
// 2
var questionTemplate = _.template(" \
<div class='card question'><span class='question'><%= question %></span> \
<ul class='options'> \
<li> \
<input type='radio' name='question[<%= index %>]' value='0' id='q<%= index %>o1'> \
<label for='q<%= index %>o1'><%= a %></label> \
</li> \
<li> \
<input type='radio' name='question[<%= index %>]' value='1' id='q<%= index %>o2'> \
<label for='q<%= index %>o2'><%= b %></label> \
</li> \
<li> \
<input type='radio' name='question[<%= index %>]' value='2' id='q<%= index %>o3'> \
<label for='q<%= index %>o3'><%= c %></label> \
</li> \
<li> \
<input type='radio' name='question[<%= index %>]' value='3' id='q<%= index %>o4'> \
<label for='q<%= index %>o4'><%= d %></label> \
</li> \
</ul> \
</div> \
");
// 3
var points,
pointsPerQuestion,
currentQuestion,
questionTimer,
timeForQuestion = 8, // seconds
timeLeftForQuestion;
// 4
$(function() {
//
$('button.start').click(start);
$('.play_again button').click(restart);
function restart() {
points = 0;
pointsPerQuestion = 10;
currentQuestion = 0;
timeLeftForQuestion = timeForQuestion;
$('.finish.card').hide();
$('div.start').show();
$('.times_up').hide();
generateCards();
updateTime();
updatePoints();
}
//
function start() {
$('div.start').fadeOut(200, function() {
moveToNextQuestion();
});
}
//
function generateCards() {
$('.questions').html('');
for (var i = 0; i < questions.length; i++) {
var q = questions[i];
var html = questionTemplate({
question: q[0],
index: i,
a: q[1],
b: q[2],
c: q[3],
d: q[4]
});
$('.questions').append(html);
};
$('.question.card input').change(optionSelected);
}
//
function moveToNextQuestion() {
currentQuestion += 1;
if (currentQuestion > 1) {
$('.question.card:nth-child(' + (currentQuestion-1) + ')').hide();
}
showQuestionCardAtIndex(currentQuestion);
setupQuestionTimer();
}
//
function setupQuestionTimer() {
if (currentQuestion > 1) {
clearTimeout(questionTimer);
}
timeLeftForQuestion = timeForQuestion;
questionTimer = setTimeout(countdownTick, 1000);
}
//
function showQuestionCardAtIndex(index) { // staring at 1
var $card = $('.question.card:nth-child(' + index + ')').show();
}
//
function countdownTick() {
timeLeftForQuestion -= 1;
updateTime();
if (timeLeftForQuestion == 0) {
return finish();
}
questionTimer = setTimeout(countdownTick, 1000);
}
//
function updateTime() {
$('.countdown .time_left').html(timeLeftForQuestion + 's');
}
//
function updatePoints() {
$('.points span.points').html(points + ' puntos');
}
//
function optionSelected() {
var selected = parseInt(this.value);
var correct = questions[currentQuestion-1][5];
if (selected == correct) {
points += pointsPerQuestion;
updatePoints();
correctAnimation();
} else {
wrongAnimation();
}
if (currentQuestion == questions.length) {
clearTimeout(questionTimer);
return finish();
}
moveToNextQuestion();
}
function correctAnimation() {
animatePoints('right');
}
//
function wrongAnimation() {
animatePoints('wrong');
}
//
function animatePoints(cls) {
$('header .points').addClass('animate ' + cls);
setTimeout(function() {
$('header .points').removeClass('animate ' + cls);
}, 500);
}
//
function finish() {
if (timeLeftForQuestion == 0) {
$('.times_up').show();
}
$('p.final_points').html(points + ' puntos');
$('.question.card:visible').hide();
$('.finish.card').show();
}
//
restart();
});