-
Notifications
You must be signed in to change notification settings - Fork 0
/
exam2.js
252 lines (208 loc) · 8.76 KB
/
exam2.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
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
/**
* @typedef {Object} QuestionOptions
* @property {string} a
* @property {string} b
* @property {string} c
* @property {string} d
*/
/**
* @typedef {Object} Question
* @property {number} number
* @property {string} description
* @property {QuestionOptions} options
* @property {number} section
* @property {string} answer
*/
/**
* @typedef {Object} SectionTitle
* @property {string} s1
* @property {string} s2
* @property {string} s3
* @property {string} s4
*/
/**
* @typedef {Object} AllQuestions
* @property {string} version
* @property {SectionTitle} sectionTitle
* @property {Object.<string, Question>} data
*/
(() => {
let count = 0;
init();
/**
* 就是初始化
*/
function init() {
const opt40 = document.querySelector("#opt40");
const opt80 = document.querySelector("#opt100");
const opt150 = document.querySelector("#opt150");
opt40.onclick = () => prepareExam(40);
opt80.onclick = () => prepareExam(100);
opt150.onclick = () => prepareExam(150);
}
/**
* 準備測驗
* @param {number} questions
*/
async function prepareExam(questions) {
// 隨機題目 id
const selectedQuestions = [];
/** @type {AllQuestions} */
const allQuestions = await fetch("raw2/result.json").then(r => r.json());
const allQuestionKeys = Object.keys(allQuestions.data);
while (selectedQuestions.length < questions) {
const thisKey = allQuestionKeys[Math.floor(Math.random() * allQuestionKeys.length)];
if (!selectedQuestions.includes(thisKey)) {
selectedQuestions.push(thisKey);
}
}
// 答題狀況
const answerResult = [];
doExam(selectedQuestions);
/**
* 開始測驗
* @param {string[]} allQuestions
* @param {number} count
*/
function doExam(allQuestionKeys, count = 0) {
const currentTitle = document.querySelector("#currentTitle");
const content = document.querySelector("#content");
const space = document.createElement("div");
space.classList.add("ts-space");
count++;
setProgress(count / allQuestionKeys.length * 100);
if (count <= allQuestionKeys.length) {
currentTitle.innerText = `第 ${count} 題`;
const key = allQuestionKeys[count - 1];
const question = allQuestions.data[key];
showQuestion(question);
} else {
showExamResult();
}
/**
* 顯示題目及綁定事件
* @param {Question} questionData
*/
function showQuestion(questionData) {
content.innerHTML = "";
// 問題描述
const description = document.createElement("div");
description.classList.add("ts-segment");
description.innerText = questionData.description;
content.append(description, space.cloneNode(), space.cloneNode());
// 送出答案
const submit = document.createElement("button");
submit.classList.add("ts-button", "is-large", "is-fluid", "is-disabled", "_submit");
submit.innerText = "送出答案";
submit.onclick = () => showQuestionResult(selectedOption, questionData.answer);
submit.disabled = true;
// 選項
let selectedOption;
["a", "b", "c", "d"].forEach(opt => {
const input = document.createElement("input");
input.type = "radio";
input.name = "option";
const textNode = document.createTextNode(questionData.options[opt]);
const label = document.createElement("label");
label.classList.add("ts-radio");
label.onclick = () => {
selectedOption = opt;
submit.disabled = false;
submit.classList.remove("is-disabled");
};
label.append(input, textNode);
content.append(label, space.cloneNode());
});
content.append(space.cloneNode(), submit);
}
/**
* 顯示單題結果
* @param {string} selected
* @param {string} answer
*/
function showQuestionResult(selected, answer) {
answerResult.push(selected);
const options = document.querySelectorAll("input[name='option']");
const submit = document.querySelector("button._submit");
const map = {
a: 0,
b: 1,
c: 2,
d: 3
};
options[map[answer]].parentElement.style.color = "var(--ts-positive-400)";
if (selected !== answer) {
options[map[selected]].parentElement.style.color = "var(--ts-negative-400)";
}
options.forEach(opt => {
opt.disabled = true;
opt.classList.add("is-disabled");
});
submit.innerText = "繼續";
submit.onclick = () => doExam(allQuestionKeys, count);
}
/**
* 顯示測驗結果
*/
function showExamResult() {
content.innerHTML = "";
// 錯誤題目的 index(根據測驗時的順序)
const wrongAnswerIndexes = [];
answerResult.forEach((ans, idx) => {
const key = allQuestionKeys[idx];
const realAnswer = allQuestions.data[key].answer;
if (ans !== realAnswer) {
wrongAnswerIndexes.push(idx);
}
});
currentTitle.innerText = "測驗結果";
const correct = questions - wrongAnswerIndexes.length;
const desc = document.createElement("div");
desc.classList.add("ts-text");
desc.innerHTML = `在 ${questions} 道題目中,你一共答對了 ${correct} 道題,答對率 <b>${parseInt(correct / questions * 100)}%</b>。`;
content.append(desc, space.cloneNode());
const desc2 = document.createElement("div");
desc2.classList.add("ts-text");
desc2.innerText = "下面是錯誤的題目:";
content.append(desc2, space.cloneNode());
wrongAnswerIndexes.forEach(ans => {
const key = allQuestionKeys[ans];
const questionData = allQuestions.data[key];
const description = document.createElement("div");
description.classList.add("ts-segment");
description.innerHTML = `
<b>(第 ${questionData.section} 章 - ${allQuestions.sectionTitle["s" + questionData.section]},第 ${questionData.number} 題)</b><br><br>
第 ${ans+1} 題 - ${questionData.description}
`;
content.append(description, space.cloneNode());
["a", "b", "c", "d"].forEach(opt => {
const text = document.createElement("div");
text.classList.add("ts-text");
text.innerText = questionData.options[opt];
if (answerResult[ans] === opt) {
text.style.color = "var(--ts-negative-400)";
}
if (questionData.answer === opt) {
text.style.color = "var(--ts-positive-400)";
}
content.append(text, space.cloneNode());
});
content.append(space.cloneNode(), space.cloneNode(), space.cloneNode());
});
const submit = document.createElement("button");
submit.classList.add("ts-button", "is-large", "is-fluid", "_submit");
submit.innerText = "再試一次";
submit.onclick = () => location.reload();
content.append(submit, space.cloneNode(), space.cloneNode(), space.cloneNode(), space.cloneNode(), space.cloneNode());
}
}
}
/**
* 設定進度條狀態
* @param {number} percent
*/
function setProgress(percent) {
const progressBar = document.querySelector("#progressBar");
progressBar.style.setProperty("--value", percent);
}
})();