-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsquiz.js
279 lines (222 loc) · 7.8 KB
/
jsquiz.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
(function() {
var questions = [{
question: "¿Que instrumento se usa para verificar la temperatura?",
choices: ["Contador Geiger", "Termómetro", "Barómetro", "Contador Gilberto"],
correctAnswer: 1
}, {
question: "¿Cual es el nombre para el medico de los taínos?",
choices: ["Cacique", "Cajaya", "Bohike", "Bohio", "Boti"],
correctAnswer: 2
}, {
question: "¿Cual huracán tuvo mas muertes?",
choices: ["George", "San Felipe II", "San Ciprián", "Santa Clara", "Hugo"],
correctAnswer: 1
}, {
question: "¿En que año llego el Huracán Hugo?",
choices: ["1998", "1988", "1989", "1996", "1987"],
correctAnswer: 2
}, {
question: "¿En que año llego el Huracán George?",
choices: ["1996", "1931", "1988", "1997", "1998"],
correctAnswer: 4
}, {
question: "¿Cuanto habitantes, segun O'Reilly, habian en San German durante el 1765?",
choices: ["Menos de 1,000", "1,000 - 2,000", "2,000 - 3,000", "4,000 - 5,000", "Mas de 5,000"],
correctAnswer: 4
}, {
question: "¿Qué porcentaje de la población en el 1765 eran esclavos africanos?",
choices: ["12.4%", "13.4%", "12.1%", "12.6%", "12%"],
correctAnswer: 3
}, {
question: "¿De la población hispana en Estados Unidos, que porcentaje son los puertoriqueños?",
choices: ["9%", "14.5%", "10.4%", "66.1%", "66.2%"],
correctAnswer: 0
}, {
question: "¿Cual es el significado de la palabra taína 'Yucahu'?",
choices: ["Dios de la Yuca", "Yuca", "Diosa de la Yucah", "Campo de Yuca", "Ensalada de Yuca"],
correctAnswer: 0
}, {
question: "¿Cual es el significado de la palabra taína 'Kayaka'?",
choices: ["Dios del Huracán", "Bote Taino", "Jefe de la Tribu", "Medico", "Bola para el batu"],
correctAnswer: 1
}, {
question: "¿Cual es el significado de la palabra taína 'Dajao'?",
choices: ["Marisco", "Salmon", "Pez Escuridiso", "Cangrejo", "Cangrejo Escuridiso"],
correctAnswer: 2
}, {
question: "¿Cual es el significado de la palabra taína 'Areito'?",
choices: ["Cangrejo Escuridiso", "Ritual para curar enfermedades", "Bote Taíno", "Sortija hecha de flores", "Baile de Los Taínos"],
correctAnswer: 4
}, {
question: "¿Quien de la tribu taína vivia en un 'Caney' y se sentaba en un 'Dujo'?",
choices: ["Bojike", "Cacique", "Bohike", "Taínos Masculinos", "Taínos Enfermos"],
correctAnswer: 1
}, {
question: "¿Cual es el significado de la palabra taína 'Jaiba'?",
choices: ["Cangrejo Escuridiso", "Medico Taíno", "Pez del Mar", "Cangrejo del Mar", "Cangrejo Femenino"],
correctAnswer: 3
}, {
question: "¿Cual de las siguientes palabras taínas significa 'Guerra Taína'?",
choices: ["Guasabara", "Batey", "Arito", "Dujo", "Batu"],
correctAnswer: 0
}];
//Guasabara
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
alert("Use your device in landscape mode for better viewing!");
}
//keep track of questions written so far
console.log(questions.length);
console.log("Hello, stranger :D");
var times = 0; //later on change this since its redundant to use it
var total;
var random = getRandomInt(0, questions.length - 1);
var questionsDone = [];
var questionCounter = 0; //Tracks question number
var badAnswers = 0;
var selections = []; //Array containing user choices
var quiz = $('#quiz'); //Quiz div object
findNext();
// Display initial question
displayNext();
// Click handler for the 'next' button
$('#next').on('click', function (e) {
e.preventDefault();
// Suspend click listener during fade animation
if(quiz.is(':animated')) {
return false;
}
choose();
// If no user selection, progress is stopped
if (isNaN(selections[random])) { //fix this error
$('#container').effect("shake");
}
else if (selections[random] !== questions[random].correctAnswer){
//alert('Incorrecto. ¡Inténtalo de nuevo!')
badAnswers++;
$('input[name="answer"]:checked').attr('disabled', true);
$('#container').effect("shake");
}
else {
times++;
questionCounter++;
if(times != questions.length)findNext();
displayNext();
}
});
// Click handler for the 'prev' button
/*$('#prev').on('click', function (e) {
e.preventDefault();
if(quiz.is(':animated')) {
return false;
}
choose();
questionCounter--;
displayNext();
});*/
// Click handler for the 'Start Over' button
$('#start').on('click', function (e) {
e.preventDefault();
if(quiz.is(':animated')) {
return false;
}
questionCounter = 0;
selections = [];
questionsDone = [];
displayNext();
total = 0;
badAnswers = 0;
times = 0;
findNext();
$('#start').hide();
});
// Animates buttons on hover
$('.button').on('mouseenter', function () {
$(this).addClass('active');
});
$('.button').on('mouseleave', function () {
$(this).removeClass('active');
});
// Creates and returns the div that contains the questions and
// the answer selections
function createQuestionElement(index) {
var qElement = $('<div>', {
id: 'question'
});
var header = $('<h2>Pregunta ' + (times + 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 fillArray(){
for(var k = 0; k < questions.length; k++){
questionsDone[k] = 0;
}
}*/
// Creates a list of the answer choices as radio inputs
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;
}
// Reads the user selection and pushes the value to an array
function choose() {
selections[random] = +$('input[name="answer"]:checked').val();
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function findNext() {
while(true){
var check = questionsDone[random];
if(!(check == 1)){
questionsDone[random] = 1;
return random = random;
}
else {
random = getRandomInt(0, questions.length - 1);
}
}
}
// Displays next requested element
function displayNext() {
quiz.fadeOut(function() {
$('#question').remove();
if(questionCounter < questions.length){
var nextQuestion = createQuestionElement(random);
quiz.append(nextQuestion).fadeIn();
if (!(isNaN(selections[random]))) {
$('input[value='+selections[random]+']').prop('checked', true);
}
if(questionCounter === 0){
$('#next').show();
}
}else {
var scoreElem = displayScore();
quiz.append(scoreElem).fadeIn();
$('#next').hide();
$('#start').show();
}
});
}
// Computes score and returns a paragraph element to be displayed
function displayScore() {
var score = $('<p>',{id: 'question'});
total = questionCounter;
total = total - badAnswers;
score.append('¡Tuvistes ' + total + ' puntos de ' +
questions.length + '!');
score.append('<p>' + '¡Cada contestacion incorrecta es -1!' + '</p>');
return score;
}
})();