forked from DiegoHerrera2/QuizMomento
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
376 lines (312 loc) · 11.4 KB
/
Copy pathscript.js
File metadata and controls
376 lines (312 loc) · 11.4 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//test
Quiz = function () {
var checkbox = false;
function begin() {
// Get number of questions from input
var questionAmount = $('#question_amount').val();
var totalQuestions = Questions.getAllQuestions().length;
if (questionAmount <= 0 || questionAmount == '') {
questionAmount = 1;
}
else if (questionAmount > totalQuestions) {
questionAmount = totalQuestions;
}
// Pick questionAmount random questions
Questions.resetRandomQuestions();
Questions.resetIncorrectRegister();
for (var i = 0; i < questionAmount; i++) {
Questions.pushRandomQuestion();
}
Questions.nextQuestion();
Counters.updateCounters();
// Display quiz
$('#container_quiz').show();
if (checkbox != true) {
$('#counter_container').hide();
}
else {
$('#counter_container').show();
}
$('#container_menu').hide();
Timer.resetTimer();
Timer.startTimer();
}
function check() {
checkbox = !checkbox;
console.log(checkbox);
}
function getCheck() {
return checkbox;
}
return {
begin: begin,
check: check,
getCheck: getCheck
}
}();
Questions = function () {
var allQuestions = new Array();
var randomQuestions = new Array();
var questionIncorrectRegister = new Array();
function loadQuestions(name = 'questions/FIS.json') {
//regex to remove questions/ from the name string
var regex = /questions\//;
var name_2 = name.replace(regex, '');
$("#selected-name").html(name_2);
allQuestions = [];
$.getJSON(name, function (data) {
allQuestions = data.questions;
});
};
function loadRandomQuestion() {
var randomQuestion = allQuestions[Math.floor(Math.random() * allQuestions.length)];
// remove the question from the array
allQuestions.splice(allQuestions.indexOf(randomQuestion), 1);
return randomQuestion;
};
function nextQuestion() {
// Check if there are any questions left
if (randomQuestions.length == CurrentQuestion.getIndex()) {
// If there are no questions left, show the results
var resultString = '<h2>Resultados</h2><br>';
resultString += '<p>Has obtenido ' + Counters.getCorrectCounter() + ' preguntas correctas y ' + Counters.getIncorrectCounter() + ' preguntas incorrectas.</p>';
resultString += '<p>Tu nota es de un ' + Math.round((Counters.getCorrectCounter() / (Counters.getCorrectCounter() + Counters.getIncorrectCounter())) * 100) + '% </p>';
resultString += '<p> En un tiempo de: ' + Timer.getMinutes() + ' minutos y ' + Timer.getSeconds() + ' segundos.</p>';
resultString += '<h3>Preguntas contestadas incorrectamente</h3>';
for(var i = 0; i < questionIncorrectRegister.length; i++) {
resultString += '<p>' + questionIncorrectRegister[i].name + '</p>';
resultString += '<p>Tu respuesta: ' + questionIncorrectRegister[i].answers[questionIncorrectRegister[i].userAnswerIndex].name + '</p>';
resultString += '<p>Respuesta correcta: ' + questionIncorrectRegister[i].answers[questionIncorrectRegister[i].answerIndex].name + '</p>';
resultString += '<br>';
}
Timer.stopTimer();
Questions.loadQuestions('questions/' + document.getElementById("selected-name").innerHTML);
showModal('Cuestionario finalizado', resultString, 'Felicidades!');
return;
};
// Load the next question
CurrentQuestion.setCurrentQuestion(randomQuestions[CurrentQuestion.getIndex()]);
CurrentQuestion.addIndex();
// Update name and question counters
$('#question_name').html(CurrentQuestion.getCurrentQuestion().name);
// Load the answers
Questions.loadAnswers();
};
function loadAnswers() {
// Clear the answers
$('#answers_container').empty();
// Load the answers
for (var i = 0; i < CurrentQuestion.getCurrentQuestion().answers.length; i++) {
var answer = CurrentQuestion.getCurrentQuestion().answers[i];
$('#answers_container').append('<button class="button" id="answer_' + i + '">' + answer.name + '</button>');
// Add click event to answer
$('#answer_' + i).click(function () {
var answerIndex = $(this).attr('id').split('_')[1];
// Check if the answer is correct by comparing index
if (CurrentQuestion.getCurrentQuestion().answerIndex == answerIndex) {
// Correct answer
Counters.addCorrect();
CurrentQuestion.getCurrentQuestion().userAnswerIndex = answerIndex;
if (Quiz.getCheck() == true) Audio.playCorrect();
} else {
// Incorrect answer
Counters.addIncorrect();
CurrentQuestion.getCurrentQuestion().userAnswerIndex = answerIndex;
questionIncorrectRegister.push(CurrentQuestion.getCurrentQuestion());
if (Quiz.getCheck() == true) Audio.playIncorrect();
}
// Update counters
if (Quiz.getCheck() == true) Counters.updateCounters();
Counters.updateTotal();
// Load the next question
Questions.nextQuestion();
});
}
}
function getAllQuestions() {
return allQuestions;
}
function getRandomQuestions() {
return randomQuestions;
}
function setAllQuestions(questions) {
allQuestions = questions;
}
function getIncorrectRegister() {
return questionIncorrectRegister;
}
function pushRandomQuestion() {
randomQuestions.push(loadRandomQuestion());
}
function resetRandomQuestions() {
randomQuestions = new Array();
}
function resetIncorrectRegister() {
questionIncorrectRegister = new Array();
}
return {
loadQuestions: loadQuestions,
loadRandomQuestion: loadRandomQuestion,
nextQuestion: nextQuestion,
loadAnswers: loadAnswers,
getAllQuestions: getAllQuestions,
getRandomQuestions: getRandomQuestions,
setAllQuestions:setAllQuestions,
getIncorrectRegister: getIncorrectRegister,
pushRandomQuestion: pushRandomQuestion,
resetRandomQuestions: resetRandomQuestions,
resetIncorrectRegister: resetIncorrectRegister
}
}();
CurrentQuestion = function () {
var currentQuestionIndex = 0;
var currentQuestion;
function getCurrentQuestion() {
return currentQuestion;
}
function addIndex(index) {
currentQuestionIndex++;
}
function getIndex() {
return currentQuestionIndex;
}
function setCurrentQuestion(question) {
currentQuestion = question;
}
function setIndex(index) {
currentQuestionIndex = index;
}
return {
getCurrentQuestion: getCurrentQuestion,
addIndex: addIndex,
getIndex: getIndex,
setCurrentQuestion: setCurrentQuestion,
setIndex: setIndex
}
}();
Counters = function () {
const correct_id = "#counter_correct";
const incorrect_id = "#counter_incorrect";
const total_id = "#counter_total";
var correct_counter = 0;
var incorrect_counter = 0;
function init() {
correct_counter = 0;
incorrect_counter = 0;
}
function addCorrect() {
correct_counter++;
}
function addIncorrect() {
incorrect_counter++;
}
function updateCounters() {
$(correct_id).html(correct_counter);
$(incorrect_id).html(incorrect_counter);
$(total_id).html(correct_counter + incorrect_counter);
}
function getCorrectCounter() {
return correct_counter;
}
function getIncorrectCounter() {
return incorrect_counter;
}
function setCorrectCounter(counter) {
correct_counter = counter;
}
function setIncorrectCounter(counter) {
incorrect_counter = counter;
}
function updateTotal() {
$(total_id).html(correct_counter + incorrect_counter);
}
return {
init: init,
addCorrect: addCorrect,
addIncorrect: addIncorrect,
updateCounters: updateCounters,
getCorrectCounter: getCorrectCounter,
getIncorrectCounter: getIncorrectCounter,
setCorrectCounter: setCorrectCounter,
setIncorrectCounter: setIncorrectCounter,
updateTotal: updateTotal
}
}();
Audio = function () {
const audio_correct = new Audio("assets/correct.mp3")
const audio_incorrect = new Audio("assets/incorrect.mp3")
//const secret_audio = new Audio("assets/secret_song.mp3")
function playCorrect() {
audio_correct.play();
}
function playIncorrect() {
audio_incorrect.play();
}
return {
playCorrect: playCorrect,
playIncorrect: playIncorrect
}
}();
// Modals
function showModal(header, content, footer) {
$('#modal-header-text').html(header);
$('#modal-body-text').html(content);
$('#modal-footer-text').html(footer);
$('#myModal').show();
// Add click event to close button
$('#modal-header-close').click(function () {
$('#myModal').hide();
// Reset counters
Counters.setCorrectCounter(0);
Counters.setIncorrectCounter(0);
// Reset question index
CurrentQuestion.setIndex(0);
// Show menu
$('#container_menu').show();
$('#container_quiz').hide();
});
}
Timer = function() {
var [milliseconds,seconds,minutes] = [0,0,0];
var timerRef = ".timerDisplay";
var int = null;
function startTimer() {
int = setInterval(function() {
milliseconds++;
if (milliseconds >= 100) {
milliseconds = 0;
seconds++;
if (seconds >= 60) {
seconds = 0;
minutes++;
}
}
$(timerRef).html(minutes + ":" + (seconds < 10 ? "0" + seconds : seconds) + ":" + (milliseconds < 10 ? "00" + milliseconds : milliseconds < 100 ? "0" + milliseconds : milliseconds));
}, 10);
}
function stopTimer() {
clearInterval(int);
}
function resetTimer() {
stopTimer();
[milliseconds,seconds,minutes] = [0,0,0];
$(timerRef).html(minutes + ":" + seconds + ":" + milliseconds);
}
function getMinutes() {
return minutes;
}
function getSeconds() {
return seconds;
}
return {
startTimer: startTimer,
resetTimer: resetTimer,
stopTimer: stopTimer,
getMinutes: getMinutes,
getSeconds: getSeconds
}
}();
// Load questions
$(document).ready(function () {
Questions.loadQuestions();
});