-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathquiz.js
71 lines (60 loc) · 2.31 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
$(document).ready(function () { // Use closure, no globals
let scores;
let current_question = 0;
initialize();
function initialize(){
scores = new Array(questions.length).fill(0);
// Shuffle Quesions
questions.sort(() => Math.random() - 0.5);
$("#btn-strongly-positive")
.click(()=>{ scores[current_question] = +1.0; next_question() });
$("#btn-positive")
.click(()=>{ scores[current_question] = +0.5; next_question() });
$("#btn-uncertain")
.click(()=>{ scores[current_question] = 0.0; next_question() });
$("#btn-negative")
.click(()=>{ scores[current_question] = -0.5; next_question() });
$("#btn-strongly-negative")
.click(()=>{ scores[current_question] = -1.0; next_question() });
$("#btn-prev").click(()=>{ prev_question() });
render_question();
}
function render_question() {
$("#question-text").html(questions[current_question].question);
$("#question-number").html(`第 ${current_question + 1} 题 剩余 ${questions.length - current_question - 1} 题`);
if (current_question == 0) {
$("#btn-prev").attr("disabled");
} else {
$("#btn-prev").removeAttr("disabled");
}
}
function next_question() {
if (current_question < questions.length - 1) {
current_question++;
render_question();
} else {
results();
}
}
function prev_question() {
if (current_question != 0) {
current_question--;
render_question();
}
}
function results() {
let score = {econ: 0, dipl: 0, govt: 0, scty: 0, envo: 0};
let max_score = {...score};
for (let i = 0; i < scores.length; i += 1 ) {
for (let key of Object.keys(score)){
score[key] += scores[i] * questions[i].effect[key];
max_score[key] += Math.abs(questions[i].effect[key]);
}
}
for (let key of Object.keys(max_score)){
score[key] = (score[key] + max_score[key]) / (2*max_score[key]);
score[key] = Math.round(score[key] * 100);
}
location.href = "results.html?" + $.param(score);
}
});