-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.html
200 lines (148 loc) · 5.85 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-99715444-7"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'UA-99715444-7');
</script>
<meta charset="utf-8">
<title>4Squares Quiz</title>
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:400,700" rel="stylesheet">
<link href='style.css' rel='stylesheet' type='text/css'>
<link rel="icon" type="x-icon" href="icon.png">
<link rel="shortcut icon" type="x-icon" href="icon.png">
<script type="application/javascript" src="questions.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1 class="title">4Squares</h1>
<svg id="svgHeader" style="width: 100%;" viewBox="0 0 112 19">
<text x="50%" y="15" class="svgHeader">4Squares</text>
</svg>
<hr>
<h2 style="text-align:center;" id="question-number">Loading...</h2>
<p class="question" id="question-text"></p>
<button class="button" onclick="next_question(1.0)" style="background-color: #1b5e20;">Strongly Agree</button> <br>
<button class="button" onclick="next_question(0.5)" style="background-color: #4caf50;">Agree</button> <br>
<button class="button" onclick="next_question(0)" style="background-color: #bbbbbb;">Neutral / Unsure</button> <br>
<button class="button" onclick="next_question(-0.5)" style="background-color: #f44336;">Disagree</button> <br>
<button class="button" onclick="next_question(-1.0)" style="background-color: #b71c1c;">Strongly Disagree</button> <br>
<button class="small_button" onclick="prev_question()" id="back_button">Back</button>
<button class="small_button_off" id="back_button_off">Back</button><br>
<script>
// variables
var answers = new Object(); // Store user's answers
var qn = 0; // Current question order
// Populate questionsObject
var questionsObject = new Object(); // Question objects with ID keys
questions.forEach(populateQO);
function populateQO(value) {
questionsObject[value['id']] = value
}
// Populate & shuffle questionsOrder
var questionsOrder = Object.keys(questionsObject); //Array of shuffled question IDs
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.get("shuffle") == "true") {
shuffleArray(questionsOrder);
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
// Question initialization
init_question();
function init_question() {
$("#question-text").html(questionsObject[questionsOrder[qn]].question);
$("#question-number").html("Question "+(qn + 1)+" of "+(questionsOrder.length));
if (jQuery.isEmptyObject(answers)) {
$('#back_button').hide()
$('#back_button_off').show()
} else {
$('#back_button').show()
$('#back_button_off').hide()
}
}
// Next question
function next_question(answer) {
if (qn === questionsOrder.length) {
return;
}
answers[questionsOrder[qn]] = answer;
qn++;
if (qn < questionsOrder.length) {
init_question();
} else {
results();
}
}
// Previous question
function prev_question() {
if (jQuery.isEmptyObject(answers)) {
$('#back_button').hide()
$('#back_button_off').show()
return;
}
qn--;
delete answers[questionsOrder[qn]];
init_question();
}
// RESULTS
function results() {
window.sessionStorage.answers = JSON.stringify(answers);
// Calculate final results
pct = percentageCalculation();
window.sessionStorage.percentages = JSON.stringify(pct);
// prepare arguments
var args = '?';
for (const i in Object.keys(pct)) {
effectName = Object.keys(pct)[i]
args += `${effectName}=${pct[effectName]}`
var cycle = parseInt(i)
if (cycle+1 !== Object.keys(pct).length) {
args += '&'
}
}
//return;
location.href = ((window.location.hostname == "testcompass.github.io") ? `feedback.html` : `results.html`) + args
}
// Calculate percentage
function percentageCalculation() {
// calc max
var max = new Object(); // Max possible scores
var score = new Object(); // User scores
var pct = new Object(); // Percentages/Score
// prepare
for (const id in answers) {
for (const effectName in questionsObject[id].effects) {
max[effectName] = 0
score[effectName] = 0
}
}
// get max & scores
for (const id in answers) {
// dismiss "don't know"
if (answers[id] !== null) {
for (const effectName in questionsObject[id].effects) {
max[effectName] += Math.abs(questionsObject[id].effects[effectName]);
score[effectName] += answers[id]*questionsObject[id].effects[effectName];
}
}
}
// calc score
for (const i in Object.keys(max)) {
effectName = Object.keys(max)[i]
if (max[effectName] > 0) {
pct[effectName] = (Math.round((score[effectName]*10/max[effectName]) * 100) / 100).toFixed(2);
} else {
pct[effectName] = 0
}
}
return pct;
}
</script>
</body>