forked from 8values/8values.github.io
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
quiz.html
183 lines (168 loc) · 7.49 KB
/
quiz.html
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
<head>
<link href='style.css' rel='stylesheet' type='text/css'>
<title data-i18n="title">8values Quiz</title>
<link rel="icon" type="x-icon" href="icon.png">
<link rel="shortcut icon" type="x-icon" href="icon.png">
<meta charset="utf-8">
<link rel="manifest" href="manifest.json" />
</head>
<body>
<button class="lang_button small_button" onclick="i18next.changeLanguage('pl'); location.reload();">Polski</button>
<button class="lang_button small_button" onclick="i18next.changeLanguage('en'); location.reload();">English</button>
<button data-i18n="switch_theme" class="theme_button small_button" onclick="switchTheme();"></button>
<h1>8values</h1>
<hr>
<h2 style="text-align:center;" id="question-number" data-i18n="question_no" data-ns="quiz">Loading…</h2>
<p class="question" id="question-text" data-ns="questions"></p>
<button class="button" onclick="nextQuestion( 1.0)" style="background-color: #1b5e20;" data-i18n="strongly_agree" data-ns="quiz"></button> <br>
<button class="button" onclick="nextQuestion( 0.5)" style="background-color: #4caf50;" data-i18n="agree" data-ns="quiz"></button> <br>
<button class="button" onclick="nextQuestion( 0.0)" style="background-color: #bbbbbb;" data-i18n="neutral" data-ns="quiz"></button> <br>
<button class="button" onclick="nextQuestion(-0.5)" style="background-color: #f44336;" data-i18n="disagree" data-ns="quiz"></button> <br>
<button class="button" onclick="nextQuestion(-1.0)" style="background-color: #b71c1c;" data-i18n="strongly_disagree" data-ns="quiz"></button> <br>
<div style="text-align: center">
<button class="small_button" style="display: initial" onclick="skipQuestion()" id="" data-i18n="answer_later">Odpowiedz później</button>
<button class="small_button" onclick="prevQuestion()" id="back_button" data-i18n="back"></button>
<button class="small_button_off" id="back_button_off" data-i18n="back"></button>
</div>
<br>
<button class="small_button small_button_alert" onclick="startOver()" id="reset_button" data-i18n="start_over"></button>
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:400,700" rel="stylesheet">
<script src="https://unpkg.com/[email protected]/i18next.min.js"></script>
<script src="https://unpkg.com/[email protected]/i18nextBrowserLanguageDetector.min.js"></script>
<script src="https://unpkg.com/[email protected]/i18nextXHRBackend.min.js"></script>
<script type="application/javascript" src="questions.js"></script>
<script type="application/javascript" src="i18n.js"></script>
<script type="application/javascript" src="themeswitcher.js"></script>
<script type="text/javascript">
themeLoad();
</script>
<!-- JavaScript for the test itself -->
<script>
i18n_load_ns(["questions", "quiz", "translation"], displayProgress)
var max_econ, max_dipl, max_govt, max_scty; // Max possible scores
max_econ = max_dipl = max_govt = max_scty = 0;
var econ, dipl, govt, scty; // User's scores
econ = dipl = govt = scty = 0;
var qn = 0; // Question number
var prevAnswer = null;
var later = [];
var areLater = false;
var state;
try {
state = JSON.parse(localStorage.getItem('state'));
} catch(err) {}
if (state) {
econ = state.econ;
dipl = state.dipl;
govt = state.govt;
scty = state.scty;
max_econ = state.max_econ;
max_dipl = state.max_dipl;
max_govt = state.max_govt;
max_scty = state.max_scty;
qn = state.qn;
later = state.later;
areLater = state.areLater;
}
function displayProgress() {
document.getElementById("question-text").dataset.i18n = questions[qn].question;
document.getElementById("question-number").dataset.current = qn + 1;
document.getElementById("question-number").dataset.total = questions.length;
i18n_ready();
if (prevAnswer == null) {
document.getElementById("back_button").style.display = 'none';
document.getElementById("back_button_off").style.display = 'initial';
} else {
document.getElementById("back_button").style.display = 'initial';
document.getElementById("back_button_off").style.display = 'none';
}
}
function saveState() {
localStorage.setItem('state', JSON.stringify({
econ: econ,
dipl: dipl,
govt: govt,
scty: scty,
max_econ: max_econ,
max_dipl: max_dipl,
max_govt: max_govt,
max_scty: max_scty,
qn: qn,
later: later,
areLater: areLater
}));
}
function nextQuestion(mult) {
econ += mult * questions[qn].effect.econ;
dipl += mult * questions[qn].effect.dipl;
govt += mult * questions[qn].effect.govt;
scty += mult * questions[qn].effect.scty;
max_econ += Math.abs(mult * questions[qn].effect.econ);
max_dipl += Math.abs(mult * questions[qn].effect.dipl);
max_govt += Math.abs(mult * questions[qn].effect.govt);
max_scty += Math.abs(mult * questions[qn].effect.scty);
qn++;
prevAnswer = mult;
if (areLater) {
later = later.slice(1);
if (later.length) {
areLater = true;
qn = later[0];
displayProgress();
saveState();
} else {
results();
}
} else if (qn < questions.length) {
displayProgress();
saveState();
} else if (later.length) {
qn = later[0];
areLater = true;
displayProgress();
saveState();
} else {
results();
}
}
function prevQuestion() {
if (prevAnswer == null) {
return;
}
qn--;
econ -= prevAnswer * questions[qn].effect.econ;
dipl -= prevAnswer * questions[qn].effect.dipl;
govt -= prevAnswer * questions[qn].effect.govt;
scty -= prevAnswer * questions[qn].effect.scty;
prevAnswer = null;
displayProgress();
}
function skipQuestion() {
later.push(qn);
if (qn === questions.length - 1) {
qn = later[0];
areLater = true;
} else {
qn++;
}
saveState();
displayProgress();
}
function startOver() {
if(confirm(i18next.t('confirm'))) {
localStorage.removeItem('state');
location.reload();
}
}
function calcScore(score, max) {
return (100 * ((score + max) / max / 2)).toFixed(1);
}
function results() {
location.href = `results.html` +
`?e=${calcScore(econ,max_econ+0.000001)}` +
`&d=${calcScore(dipl,max_dipl+0.000001)}` +
`&g=${calcScore(govt,max_govt+0.000001)}` +
`&s=${calcScore(scty,max_scty+0.000001)}`;
}
</script>
</body>