forked from GeenenTijd/CoderDojoJavascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.js
130 lines (114 loc) · 4.4 KB
/
questions.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
include(['vars.questions', 'ifelse.questions', 'loops.questions', 'functions.questions'], function (varq, ifelseq, loopsq, functionsq) {
'use strict';
var self = {
currentCategory: null,
currentQuestion: 0,
question: null,
nextAllowed: false,
loadQuestion: loadQuestion,
saveQuestion: saveQuestion
};
function verifyPermission() {
if ('localStorage' in window && window['localStorage'] !== null) {
var storageVar = localStorage[self.currentCategory];
if (typeof storageVar === 'undefined') {
return self.currentQuestion === 0;
}
var storageQuestion = parseInt(storageVar);
if (self.currentQuestion < storageQuestion) {
self.nextAllowed = true;
}
if (self.currentQuestion === 0) {
return true;
}
return self.currentQuestion <= storageQuestion;
} else if (self.currentQuestion === 0) {
return true;
}
return false;
}
function validCategory() {
return self.currentCategory === 'variables' || self.currentCategory === 'ifelse' || self.currentCategory === 'loops' || self.currentCategory === 'functions';
}
function retrieveQuestion() {
if (self.currentCategory === 'variables') {
if (self.currentQuestion < varq.length) {
self.question = varq[self.currentQuestion];
} else {
self.question = null;
}
} else if (self.currentCategory === 'ifelse') {
if (self.currentQuestion < ifelseq.length) {
self.question = ifelseq[self.currentQuestion];
} else {
self.question = null;
}
} else if (self.currentCategory === 'loops') {
if (self.currentQuestion < loopsq.length) {
self.question = loopsq[self.currentQuestion];
} else {
self.question = null;
}
} else if (self.currentCategory === 'functions') {
if (self.currentQuestion < functionsq.length) {
self.question = functionsq[self.currentQuestion];
} else {
self.question = null;
}
} else {
self.question = null;
}
return self.question;
}
function loadQuestion() {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] === 'category') {
self.currentCategory = pair[1];
} else if (pair[0] === 'question') {
self.currentQuestion = parseInt(pair[1]);
}
}
self.nextAllowed = false;
if (!validCategory()) {
self.currentCategory = 'variables';
}
if (verifyPermission()) {
return retrieveQuestion();
} else {
if ('localStorage' in window && window['localStorage'] !== null) {
var storageVar = localStorage[self.currentCategory];
if (typeof storageVar === 'undefined') {
self.currentQuestion = 0;
} else {
self.currentQuestion = parseInt(storageVar);
}
self.currentQuestion = parseInt(localStorage[self.currentCategory]);
} else {
self.currentQuestion = 0;
}
history.replaceState(null, '', 'questions.html?category=' + self.currentCategory + '&question=' + self.currentQuestion);
return retrieveQuestion();
}
}
function save() {
localStorage.setItem(self.currentCategory, self.currentQuestion);
}
function saveQuestion() {
++self.currentQuestion;
if ('localStorage' in window && window['localStorage'] !== null) {
var storageVar = localStorage[self.currentCategory];
if (typeof storageVar === 'undefined') {
if (self.currentQuestion === 1) {
save();
}
} else if (parseInt(storageVar) < self.currentQuestion) {
save();
}
}
history.pushState(null, '', 'questions.html?category=' + self.currentCategory + '&question=' + self.currentQuestion);
}
return self;
});