Skip to content

Commit 80b2ca8

Browse files
Taylor O'NeillGenora51
Taylor O'Neill
authored andcommitted
Allow going back multiple times
1 parent 8b8ef87 commit 80b2ca8

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

Diff for: quiz.html

+21-20
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ <h2 style="text-align:center;" id="question-number">Loading...</h2>
5050
<script>
5151
var max_econ, max_dipl, max_govt, max_scty; // Max possible scores
5252
max_econ = max_dipl = max_govt = max_scty = 0;
53-
var econ, dipl, govt, scty; // User's scores
54-
econ = dipl = govt = scty = 0;
53+
let econ_array = new Array(questions.length);
54+
let dipl_array = new Array(questions.length);
55+
let govt_array = new Array(questions.length);
56+
let scty_array = new Array(questions.length);
5557
var qn = 0; // Question number
56-
var prev_answer = null;
5758
init_question();
5859
for (var i = 0; i < questions.length; i++) {
5960
max_econ += Math.abs(questions[i].effect.econ)
@@ -64,7 +65,7 @@ <h2 style="text-align:center;" id="question-number">Loading...</h2>
6465
function init_question() {
6566
document.getElementById("question-text").innerHTML = questions[qn].question;
6667
document.getElementById("question-number").innerHTML = "Question " + (qn + 1) + " of " + (questions.length);
67-
if (prev_answer == null) {
68+
if (qn == 0) {
6869
document.getElementById("back_button").style.display = 'none';
6970
document.getElementById("back_button_off").style.display = 'block';
7071
} else {
@@ -75,40 +76,40 @@ <h2 style="text-align:center;" id="question-number">Loading...</h2>
7576
}
7677

7778
function next_question(mult) {
78-
econ += mult*questions[qn].effect.econ
79-
dipl += mult*questions[qn].effect.dipl
80-
govt += mult*questions[qn].effect.govt
81-
scty += mult*questions[qn].effect.scty
79+
econ_array[qn] = mult*questions[qn].effect.econ
80+
dipl_array[qn] = mult*questions[qn].effect.dipl
81+
govt_array[qn] = mult*questions[qn].effect.govt
82+
scty_array[qn] = mult*questions[qn].effect.scty
8283
qn++;
83-
prev_answer = mult;
8484
if (qn < questions.length) {
8585
init_question();
8686
} else {
8787
results();
8888
}
8989
}
90+
9091
function prev_question() {
91-
if (prev_answer == null) {
92+
if (qn == 0) {
9293
return;
9394
}
9495
qn--;
95-
econ -= prev_answer * questions[qn].effect.econ;
96-
dipl -= prev_answer * questions[qn].effect.dipl;
97-
govt -= prev_answer * questions[qn].effect.govt;
98-
scty -= prev_answer * questions[qn].effect.scty;
99-
prev_answer = null;
10096
init_question();
101-
10297
}
98+
10399
function calc_score(score,max) {
104100
return (100*(max+score)/(2*max)).toFixed(1)
105101
}
102+
106103
function results() {
104+
let final_econ = econ_array.reduce((a, b) => a + b, 0)
105+
let final_dipl = dipl_array.reduce((a, b) => a + b, 0)
106+
let final_govt = govt_array.reduce((a, b) => a + b, 0)
107+
let final_scty = scty_array.reduce((a, b) => a + b, 0)
107108
location.href = `results.html`
108-
+ `?e=${calc_score(econ,max_econ)}`
109-
+ `&d=${calc_score(dipl,max_dipl)}`
110-
+ `&g=${calc_score(govt,max_govt)}`
111-
+ `&s=${calc_score(scty,max_scty)}`
109+
+ `?e=${calc_score(final_econ,max_econ)}`
110+
+ `&d=${calc_score(final_dipl,max_dipl)}`
111+
+ `&g=${calc_score(final_govt,max_govt)}`
112+
+ `&s=${calc_score(final_scty,max_scty)}`
112113
}
113114
</script>
114115
</body>

0 commit comments

Comments
 (0)