-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
144 lines (122 loc) · 4.07 KB
/
index.js
File metadata and controls
144 lines (122 loc) · 4.07 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
let questions = [];
let userAnswers = [];
let current = 0;
const elCurrentNum = document.getElementById('current-num');
const elDoneCount = document.getElementById('done-count');
const elQuestionTitle = document.getElementById('question-title');
const elOptions = document.getElementById('options');
const elPrev = document.getElementById('prev');
const elNext = document.getElementById('next');
const elSubmitAll = document.getElementById('submit-all');
const elJumpInput = document.getElementById('jump-input');
const elJumpBtn = document.getElementById('jump-btn');
const elScoreBox = document.getElementById('score-box');
const elLevelText = document.getElementById('level-text');
(async () => {
try {
const response = await fetch('questions.json');
questions = await response.json();
userAnswers = Array(questions.length).fill(null);
render();
} catch (error) {
console.error("题库加载失败:", error);
elQuestionTitle.textContent = "题库加载失败,请检查 questions.json 文件。";
}
})();
function render() {
if (questions.length === 0) return;
const q = questions[current];
elCurrentNum.textContent = current + 1;
elQuestionTitle.textContent = `${current + 1}. ${q.title}`;
elOptions.innerHTML = '';
q.options.forEach((opt, i) => {
const div = document.createElement('div');
div.className = 'option ' + (userAnswers[current] === i ? 'selected' : '');
div.textContent = `${String.fromCharCode(65 + i)}. ${opt}`;
div.onclick = () => {
userAnswers[current] = i;
render();
updateDoneCount();
};
elOptions.appendChild(div);
});
elPrev.disabled = current === 0;
elNext.disabled = current === questions.length - 1;
updateDoneCount();
}
function updateDoneCount() {
const count = userAnswers.filter(x => x !== null).length;
elDoneCount.textContent = count;
}
function calcScore() {
const categoryScores = {
basic: 0, hardware: 0, os: 0, tools: 0, network: 0,
ops: 0, frontend: 0, backend: 0, media: 0, solve: 0
};
questions.forEach((q, i) => {
if (userAnswers[i] === q.answer) {
if (categoryScores.hasOwnProperty(q.type)) {
categoryScores[q.type]++;
}
}
});
const total = Object.values(categoryScores).reduce((a, b) => a + b, 0);
for (const [category, score] of Object.entries(categoryScores)) {
const span = document.getElementById(`score-${category}`);
if (span) span.textContent = score;
}
document.getElementById('score-total').textContent = total;
let level = '';
let levelClass = '';
if (total >= 90) {
level = '夯';
levelClass = 'level-top';
} else if (total >= 80) {
level = '顶尖';
levelClass = 'level-ding';
} else if (total >= 60) {
level = '人上人';
levelClass = 'level-hang';
} else if (total >= 40) {
level = 'NPC';
levelClass = 'level-npc';
} else {
level = '拉';
levelClass = 'level-rula';
}
elLevelText.className = 'level-text ' + levelClass;
elLevelText.textContent = '技术评级:' + level;
elScoreBox.style.display = 'block';
elScoreBox.scrollIntoView({ behavior: 'smooth' });
}
elPrev.onclick = () => {
if (current > 0) {
current--;
render();
}
};
elNext.onclick = () => {
if (current < questions.length - 1) {
current++;
render();
}
};
elSubmitAll.onclick = () => {
const remaining = questions.length - userAnswers.filter(x => x !== null).length;
if (remaining > 0) {
if (confirm(`还有 ${remaining} 道题未完成,确定要交卷吗?`)) {
calcScore();
}
} else {
calcScore();
}
};
elJumpBtn.onclick = () => {
const val = parseInt(elJumpInput.value);
if (val >= 1 && val <= questions.length) {
current = val - 1;
render();
} else {
alert(`请输入 1 到 ${questions.length} 之间的题号`);
}
};