-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathquiz.js
162 lines (136 loc) Β· 3.74 KB
/
quiz.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
language = localStorage.getItem("language") || navigator.language || navigator.userLanguage;
// Choosing right translation file
jQuery.loadScript = function (url, callback) {
jQuery.ajax({
url: url,
dataType: 'script',
success: callback,
async: false
});
}
$.loadScript('./langs/' + language + '/questions.js', function(){
var now = new Date();
var startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var timestamp = startOfDay / 1000;
// We shuffle questions once they have been dl with a different seed every day
shuffle(questions, timestamp);
});
start_time();
function init_quiz() {
qn = 0; // Question number
prev_answer = null;
init_question();
}
// Count time passed on the quiz
function start_time() {
startTime = new Date();
};
function end_time() {
endTime = new Date();
var timeDiff = endTime - startTime; //in ms
// strip the ms
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff);
return(seconds);
}
function shuffle(array, seed) {
let currentIndex = array.length, temporaryValue, randomIndex;
seed = seed || 1;
let random = function() {
var x = Math.sin(seed++) * 10000;
return x - Math.floor(x);
};
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function init_question() {
question_string = $.i18n("question")
ques_of_string = $.i18n("of")
document.getElementById("question-text").innerHTML = questions[qn].question;
document.getElementById(
"question-number"
).innerHTML = question_string +" "+ (qn + 1) +" "+ ques_of_string +" "+ questions.length
if (qn == 0) {
document.getElementById("back_button").style.display = "none";
document.getElementById("back_button_off").style.display = "block";
} else {
document.getElementById("back_button").style.display = "block";
document.getElementById("back_button_off").style.display = "none";
}
}
function next_question(mult) {
questions[qn].answer = mult;
qn++;
if (qn < questions.length) {
init_question();
} else {
results();
}
}
function prev_question() {
if (qn == 0) {
return;
}
qn--;
init_question();
}
function calc_score(score, max_value) {
return ((100 * score) / max_value).toFixed(0);
}
function results() {
var axes = {};
for (var i = 0; i < questions.length; i++) {
var q = questions[i];
for (var j = 0; j < q.valuesYes.length; j++) {
var a = q.valuesYes[j];
if (!(a.axis in axes)) {
axes[a.axis] = {
val: 0,
sum: 0
};
}
if (q.answer > 0) {
axes[a.axis].val += q.answer * a.value;
}
axes[a.axis].sum += Math.max(a.value, 0);
}
for (var j = 0; j < q.valuesNo.length; j++) {
var a = q.valuesNo[j];
if (!(a.axis in axes)) {
axes[a.axis] = {
val: 0,
sum: 0
};
}
if (q.answer < 0) {
axes[a.axis].val -= q.answer * a.value;
}
axes[a.axis].sum += Math.max(a.value, 0);
}
}
var url = "";
for (var aK in axes) {
if (axes[aK].val > 0) {
if (url != "") url += "&";
url += aK + "=" + calc_score(axes[aK].val, axes[aK].sum);
}
}
base64_url = window.btoa(url);
quiz_time = end_time();
$.post("save",{"data": base64_url, "time": quiz_time})
.always(function() {
url = "./results?" + base64_url;
console.log("It tooks " + quiz_time + " seconds");
location.href = url;
});
}