-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
259 lines (229 loc) · 9.92 KB
/
script.js
File metadata and controls
259 lines (229 loc) · 9.92 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
document.addEventListener('DOMContentLoaded', () => {
// DOM Elements
const fileInput = document.getElementById('file-input');
const startBtn = document.getElementById('start-btn');
const addRowBtn = document.getElementById('add-row-btn');
const tableBody = document.getElementById('table-body');
const footer = document.querySelector('footer');
const uploadSection = document.getElementById('upload-section');
const flashcardSection = document.getElementById('flashcard-section');
const spellingSection = document.getElementById('spelling-section');
const summarySection = document.getElementById('summary-section');
const navFlashcard = document.getElementById('nav-flashcard');
const navSpelling = document.getElementById('nav-spelling');
// Flashcard Elements
const flashcardWord = document.getElementById('flashcard-word');
const option1 = document.getElementById('option1');
const option2 = document.getElementById('option2');
const flashcardResult = document.getElementById('flashcard-result');
// Spelling Elements
const spellingTranslation = document.getElementById('spelling-translation');
const spellingInput = document.getElementById('spelling-input');
const checkSpellingBtn = document.getElementById('check-spelling-btn');
const spellingResult = document.getElementById('spelling-result');
// Summary Elements
const accuracyText = document.getElementById('accuracy-text');
const restartBtn = document.getElementById('restart-btn');
// App State
let words = [];
let currentWordIndex = 0;
let correctAnswers = 0;
let currentMode = 'flashcard'; // 'flashcard' or 'spelling'
let shuffledWords = [];
// --- Event Listeners ---
startBtn.addEventListener('click', startGame);
addRowBtn.addEventListener('click', addTableRow);
navFlashcard.addEventListener('click', () => switchMode('flashcard'));
navSpelling.addEventListener('click', () => switchMode('spelling'));
restartBtn.addEventListener('click', restartGame);
// --- Core Functions ---
function parseWordsFromFile(text) {
const lines = text.trim().split('\n');
const regex = /<([^>]+)>\s*<([^>]+)>\s*<([^>]+)>/;
return lines.map(line => {
const match = line.match(regex);
if (match) {
return {
english: match[1].trim(),
correct: match[2].trim(),
incorrect: match[3].trim()
};
}
return null;
}).filter(word => word !== null);
}
function parseWordsFromTable() {
const rows = tableBody.querySelectorAll('.table-row');
const wordsFromTable = [];
rows.forEach(row => {
const english = row.querySelector('.english-input').value.trim();
const correct = row.querySelector('.correct-input').value.trim();
const incorrect = row.querySelector('.incorrect-input').value.trim();
if (english && correct && incorrect) {
wordsFromTable.push({ english, correct, incorrect });
}
});
return wordsFromTable;
}
function addTableRow() {
const newRow = document.createElement('div');
newRow.className = 'table-row';
newRow.innerHTML = `
<input type="text" class="english-input" placeholder="e.g., apple">
<input type="text" class="correct-input" placeholder="e.g., n.苹果">
<input type="text" class="incorrect-input" placeholder="e.g., n.香蕉">
`;
tableBody.appendChild(newRow);
}
function startGame() {
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
words = parseWordsFromFile(e.target.result);
if (words.length > 0) {
initializeGame();
} else {
alert('文件格式不正确或内容为空!');
}
};
reader.readAsText(file);
} else {
words = parseWordsFromTable();
if (words.length > 0) {
initializeGame();
} else {
alert('请至少输入一个完整的单词行或上传文件!');
}
}
}
function initializeGame() {
shuffledWords = [...words].sort(() => Math.random() - 0.5);
currentWordIndex = 0;
correctAnswers = 0;
uploadSection.classList.add('hidden');
summarySection.classList.add('hidden');
footer.classList.remove('hidden'); // Show footer navigation
switchMode(currentMode);
}
function switchMode(mode) {
currentMode = mode;
flashcardSection.classList.add('hidden');
spellingSection.classList.add('hidden');
navFlashcard.classList.remove('active');
navSpelling.classList.remove('active');
if (mode === 'flashcard') {
flashcardSection.classList.remove('hidden');
navFlashcard.classList.add('active');
setupFlashcards();
} else {
spellingSection.classList.remove('hidden');
navSpelling.classList.add('active');
setupSpelling();
}
}
function setupFlashcards() {
currentWordIndex = 0;
correctAnswers = 0;
shuffledWords = [...words].sort(() => Math.random() - 0.5);
showNextWord();
}
function setupSpelling() {
currentWordIndex = 0;
correctAnswers = 0;
// For spelling, we might want a different shuffle logic, but for now, it's the same.
shuffledWords = [...words].sort(() => Math.random() - 0.5);
showNextWord();
}
function showNextWord() {
if (currentWordIndex >= shuffledWords.length) {
showSummary();
return;
}
const word = shuffledWords[currentWordIndex];
if (currentMode === 'flashcard') {
flashcardResult.classList.add('hidden');
flashcardWord.textContent = word.english;
const options = [word.correct, word.incorrect].sort(() => Math.random() - 0.5);
option1.textContent = options[0];
option2.textContent = options[1];
option1.onclick = () => checkFlashcardAnswer(options[0]);
option2.onclick = () => checkFlashcardAnswer(options[1]);
} else if (currentMode === 'spelling') {
spellingResult.textContent = '';
spellingResult.className = '';
spellingTranslation.textContent = word.correct;
spellingInput.value = '';
spellingInput.focus();
checkSpellingBtn.onclick = checkSpellingAnswer;
spellingInput.onkeyup = (e) => {
if (e.key === 'Enter') {
checkSpellingAnswer();
}
};
}
}
function checkFlashcardAnswer(selectedOption) {
const correct = selectedOption === shuffledWords[currentWordIndex].correct;
flashcardResult.classList.remove('hidden');
if (correct) {
correctAnswers++;
flashcardResult.textContent = '正确! ✅';
flashcardResult.className = 'correct';
setTimeout(() => {
currentWordIndex++;
showNextWord();
}, 1000);
} else {
flashcardResult.textContent = `错误! ❌ 正确答案: ${shuffledWords[currentWordIndex].correct}`;
flashcardResult.className = 'incorrect';
// Play pronunciation (not implemented, placeholder)
// const utterance = new SpeechSynthesisUtterance(shuffledWords[currentWordIndex].english);
// speechSynthesis.speak(utterance);
setTimeout(() => {
currentWordIndex++;
showNextWord();
}, 2000);
}
}
function checkSpellingAnswer() {
const userAnswer = spellingInput.value.trim();
const correctAnswer = shuffledWords[currentWordIndex].english;
if (userAnswer.toLowerCase() === correctAnswer.toLowerCase()) {
correctAnswers++;
spellingResult.textContent = '正确! ✅';
spellingResult.className = 'correct';
setTimeout(() => {
currentWordIndex++;
showNextWord();
}, 1000);
} else {
spellingResult.textContent = `错误! ❌ 正确拼写: ${correctAnswer}`;
spellingResult.className = 'incorrect';
spellingInput.value = ''; // Clear input for re-try
spellingInput.focus();
}
}
function showSummary() {
flashcardSection.classList.add('hidden');
spellingSection.classList.add('hidden');
summarySection.classList.remove('hidden');
const accuracy = words.length > 0 ? (correctAnswers / words.length * 100).toFixed(1) : 0;
accuracyText.textContent = `本次练习共 ${words.length} 个单词,答对 ${correctAnswers} 个,正确率: ${accuracy}%`;
}
function restartGame() {
uploadSection.classList.remove('hidden');
summarySection.classList.add('hidden');
footer.classList.add('hidden');
words = [];
fileInput.value = '';
// Clear and reset table
tableBody.innerHTML = `
<div class="table-row">
<input type="text" class="english-input" placeholder="e.g., apple">
<input type="text" class="correct-input" placeholder="e.g., n.苹果">
<input type="text" class="incorrect-input" placeholder="e.g., n.香蕉">
</div>
`;
}
});