-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstroop.js
More file actions
326 lines (293 loc) · 7.79 KB
/
stroop.js
File metadata and controls
326 lines (293 loc) · 7.79 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
var START_TIME = 0;
var WORD = ["紅", "藍", "綠", "黃"];
var WORD2 = ["騿", "宸", "教", "曜"];
var COLOR = ["red", "blue", "green", "amber"];
var CURRENT_QUESTION_NUM = 0;
var STROOP_TOTAL_QUESTION_NUM = localStorage.getItem(
"STROOP_TOTAL_QUESTION_NUM"
);
var registerModalInstance;
// ---------------彈出視窗(Modal)---------------
document.addEventListener("DOMContentLoaded", function () {
let registerModal = document.getElementById("modal-register");
registerModalInstance = M.Modal.init(registerModal, { opacity: 0.5 });
});
let el_inputName = document.getElementById("input-name");
el_inputName.addEventListener("keypress", function (e) {
let key = e.which || e.keyCode;
if (key === 13) {
register();
}
});
function setCurrentMode(mode) {
localStorage.setItem("CURRENT_MODE", mode);
window.location.href = "./stroop.html";
}
function showH3() {
let CURRENT_SEQUENCE = parseInt(localStorage.getItem("CURRENT_SEQUENCE"));
let content = "";
if (CURRENT_SEQUENCE == -1) {
content = "練習";
} else {
switch (parseInt(localStorage.getItem("CURRENT_MODE"))) {
case 1:
content = "模式一";
break;
case 2:
content = "模式二";
break;
case 3:
content = "模式三";
break;
case 4:
content = "模式四";
break;
case 5:
content = "模式五";
break;
}
}
document.getElementById("h3-show").innerHTML = content;
}
/**
* 初始化
* @param 畫面名稱 type
*/
function init(type) {
try {
clearInterval(totalInterval);
clearInterval(qInterval);
} catch (e) {
// do nothing...
}
document.getElementById("btn-start").addEventListener("click", function () {
start(type);
show();
});
showH3();
}
function goSaveUser() {
let savedUser = localStorage.getItem("savedUser");
if (savedUser === null) {
savedUser = [];
} else {
savedUser = JSON.parse(savedUser);
}
let currentUser = localStorage.getItem("currentUser");
if (currentUser) {
currentUser = JSON.parse(currentUser);
savedUser.push(currentUser);
localStorage.setItem("savedUser", JSON.stringify(savedUser));
}
localStorage.setItem("CURRENT_SEQUENCE", -1);
localStorage.setItem("CURRENT_MODE", 1);
localStorage.setItem("currentUser", JSON.stringify({}));
window.location.href = "./index-stroop.html";
}
/**
* 開始測驗
* @param 畫面名稱 type
*/
function start() {
START_TIME = new Date().getTime();
// 改變畫面
document.getElementById("startBlock").style.display = "none";
document.getElementById("questionBlock").style.display = "block";
}
var failNumArray = [];
var failNum = 0;
/**
* 計算得分
* @param x
* @param y
*/
function score(x, y) {
if (x == y) {
STROOP_TOTAL_QUESTION_NUM -= 1;
if (STROOP_TOTAL_QUESTION_NUM) {
failNumArray.push(failNum);
failNum = 0;
show();
} else {
finish("stroop");
}
} else {
failNum += 1;
}
}
/**
* 結束測驗,顯示結果
* @param 畫面名稱 type
*/
function finish(type) {
// 改變畫面
END_TIME = new Date().getTime();
spendTime = (END_TIME - START_TIME) / 1000;
document.getElementById("questionBlock").style.display = "none";
document.getElementById("resultBlock").style.display = "block";
document.getElementById("result").innerHTML = `
共失誤 ${failNumArray.reduce((a, b) => a + b)} 次
<br>
花費 ${spendTime} 秒
`;
if (localStorage.getItem("currentUser") !== null) {
// 儲存成績
let currentUser = localStorage.getItem("currentUser");
currentUser = JSON.parse(currentUser);
currentUser.scores.push({
mode: localStorage.getItem("CURRENT_MODE"),
type: type,
questionNum: localStorage.getItem("STROOP_TOTAL_QUESTION_NUM"),
failNumArray: failNumArray,
endTime: new Date().toLocaleString(),
spendTime: spendTime,
});
localStorage.setItem("currentUser", JSON.stringify(currentUser));
}
}
/**
* 註冊
*/
function register() {
if (el_inputName.value === "") {
return false;
} else {
var formReg = document.forms["form-register"];
localStorage.setItem(
"currentUser",
JSON.stringify({
name: formReg.elements["input-name"].value,
isPreTest: Boolean(formReg.elements["test1"].value),
scores: [],
testTime: new Date().toLocaleString(),
})
);
localStorage.setItem("CURRENT_SEQUENCE", 0);
localStorage.setItem("CURRENT_MODE", 1);
registerModalInstance.close();
window.location.href = "./stroop.html";
}
}
//------------------------顯示題目------------------------
var COLOR_PREV = "";
var WORD_PREV = "";
var WORD2_PREV = "";
function getMode1Question() {
while (true) {
[q_num] = getRandomNumbers([0, 1, 2, 3], 1);
if (WORD_PREV != WORD[q_num]) {
break;
}
}
WORD_PREV = WORD[q_num];
CURRENT_QUESTION_NUM = q_num;
return ["black", WORD[q_num]];
}
function getMode2Question() {
while (true) {
[q_num] = getRandomNumbers([0, 1, 2, 3], 1);
if (COLOR_PREV != COLOR[q_num] && WORD_PREV != WORD[q_num]) {
break;
}
}
COLOR_PREV = COLOR[q_num];
WORD_PREV = WORD[q_num];
CURRENT_QUESTION_NUM = q_num;
return [COLOR[q_num], WORD[q_num]];
}
function getMode3Question() {
while (true) {
[q_num] = getRandomNumbers([0, 1, 2, 3], 1);
if (COLOR_PREV != COLOR[q_num]) {
break;
}
}
COLOR_PREV = COLOR[q_num];
CURRENT_QUESTION_NUM = q_num;
return [COLOR[q_num], ""];
}
function getMode4Question() {
while (true) {
[color_num, word_num] = getRandomNumbers([0, 1, 2, 3], 2);
if (COLOR_PREV != COLOR[color_num] && WORD_PREV != WORD[word_num]) {
break;
}
}
COLOR_PREV = COLOR[color_num];
WORD_PREV = WORD[word_num];
CURRENT_QUESTION_NUM = color_num;
return [COLOR[color_num], WORD[word_num]];
}
function getMode5Question() {
while (true) {
[color_num, word_num] = getRandomNumbers([0, 1, 2, 3], 2);
if (COLOR_PREV != COLOR[color_num] && WORD2_PREV != WORD2[word_num]) {
break;
}
}
COLOR_PREV = COLOR[color_num];
WORD_PREV = WORD2[word_num];
CURRENT_QUESTION_NUM = color_num;
return [COLOR[color_num], WORD2[word_num]];
}
function show() {
let [color, word] = [];
let innerHTML = "";
switch (parseInt(localStorage.getItem("CURRENT_MODE"))) {
case 1:
[color, word] = getMode1Question();
break;
case 2:
[color, word] = getMode2Question();
break;
case 4:
[color, word] = getMode4Question();
break;
case 5:
[color, word] = getMode5Question();
break;
}
innerHTML = `
<span id="color-question" class="${color}-text">
${word}
</span>
`;
document.getElementById("div-question").innerHTML = innerHTML;
if (parseInt(localStorage.getItem("CURRENT_MODE")) === 3) {
[color, word] = getMode3Question();
innerHTML = `
<div class="row">
<div class="col offset-s4 s4">
<div class="card ${color}">
<div class="card-content" style="height: 40vh;"></div>
</div>
</div>
</div>
`;
document.getElementById("div-question").innerHTML = innerHTML;
}
}
//------------------------點擊答案,計算分數並顯示新題目------------------------
function submitAns(x) {
score(x, COLOR[CURRENT_QUESTION_NUM]);
}
// ---------------下載測驗結果---------------
document.getElementById("btn-download").addEventListener("click", function () {
goSaveUser();
download();
});
let el_STROOP_TOTAL_QUESTION_NUM = document.getElementById(
"STROOP_TOTAL_QUESTION_NUM"
);
function loadSetting() {
el_STROOP_TOTAL_QUESTION_NUM.value = localStorage.getItem(
"STROOP_TOTAL_QUESTION_NUM"
);
}
function saveSetting() {
localStorage.setItem(
"STROOP_TOTAL_QUESTION_NUM",
el_STROOP_TOTAL_QUESTION_NUM.value
);
M.toast({ html: "儲存成功!" });
}