-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
350 lines (269 loc) · 11.9 KB
/
index.html
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css"
integrity="sha512-Kc323vGBEqzTmouAECnVceyQqyqdsSiqLQISBL29aUW4U/M7pSPA/gEUZQqv1cwx4OnYxTxve5UMg5GT6L4JJg=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="style.css">
<link rel="icon" href="favicon.png">
<title>Vowel Quiz</title>
<style>
</style>
</head>
<body>
<div class="start-page">
<h1>Welcome to the Vowel Quiz!</h1>
<p>Test your knowledge of the International Phonetic Alphabet (<a href="javascript:void(0);"
onclick="window.open('https://en.wikipedia.org/wiki/IPA_vowel_chart_with_audio', '_blank', 'width=400px,height=600px,top=100px,left=500px');">
<abbr title="International Phonetic Alphabet">IPA</abbr></a>) by identifying vowel sounds.
Listen carefully, choose the correct symbol, and improve your phonetic skills!</p>
<p>Press Play to start the quiz and challenge yourself with different vowel sounds.</p>
<button class="button" id="startQuizBtn">Play</button>
</div>
<div class="quiz-container">
<div class="main-container">
<div class="timer" id="timer">Time remaining: <span id="minutes">10</span>:<span id="seconds">00</span>
</div>
<div class="score-container">
Score: <span id="score">0</span>/10
</div>
<div id="questionContainer" class="question-container">
<div class="question">
<h2>
Match the <a href="javascript:void(0);"
onclick="window.open('https://en.wikipedia.org/wiki/IPA_vowel_chart_with_audio', '_blank', 'width=400px,height=600px,top=100px,left=500px');">
<abbr title="International Phonetic Alphabet">IPA</abbr></a> vowel:
</h2>
</div>
<div class="audio-player">
<i class="fa-solid fa-volume-high" id="audioIcon"></i>
<audio id="audioPlayer" controls>
<!-- The source will be set dynamically -->
</audio>
</div>
<p id="response">Response</p>
<div class="answers-container" id="answersContainer">
<!-- The answers will be generated dynamically -->
</div>
<button class="button" id="checkAnswerBtn">Check</button>
<button class="button" id="nextQuestionBtn" disabled>Next</button>
</div>
</div>
</div>
<div id="modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p id="modalText">Time is almost up! 1 minute left!</p>
</div>
</div>
<script>
String.Empty = "";
function $(selector) {
return document.querySelector(selector);
}
HTMLElement.prototype.on = function (event, action) {
this.addEventListener(event, action);
return this;
};
HTMLElement.prototype.click = function (action) {
this.addEventListener("click", action);
return this;
};
function swap(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
var $startPage = $(".start-page");
var $quizContainer = $(".quiz-container");
var $mainContainer = $(".main-container");
var $scoreDisplay = $("#score");
var $minutesDisplay = $("#minutes");
var $secondsDisplay = $("#seconds");
var $answersContainer = $("#answersContainer");
var $response = $("#response");
var $nextQuestionBtn = $("#nextQuestionBtn");
var $checkAnswerBtn = $("#checkAnswerBtn");
var $audioPlayer = $("#audioPlayer");
var $audioIcon = $("#audioIcon");
var score = 0;
var currentQuestionIndex = 0;
var questionsCount = 10;
var userMistakeList = [];
var currentVowel = null;
var vowels = [
{
vowel: "i"
, audio: "vowels/i_Close_front_unrounded_vowel.mp3"
, link: "https://en.wikipedia.org/wiki/Close_front_unrounded_vowel"
}, {
vowel: "u"
, audio: "vowels/u_Close_back_rounded_vowel.mp3"
, link: "https://en.wikipedia.org/wiki/Close_back_rounded_vowel"
}, {
vowel: "o"
, audio: "vowels/o_Close-mid_back_rounded_vowel.mp3"
, link: "https://en.wikipedia.org/wiki/Close-mid_back_rounded_vowel"
}, {
vowel: "ə"
, audio: "vowels/ə_Mid-central_vowel.mp3"
, link: "https://en.wikipedia.org/wiki/mid_central_vowel"
}, {
vowel: "a"
, audio: "vowels/a_PR-open_front_unrounded_vowel.mp3"
, link: "https://en.wikipedia.org/wiki/Open_front_unrounded_vowel"
}, {
vowel: "ɛ"
, audio: "vowels/ɛ_Open-mid_front_unrounded_vowel.mp3"
, link: "https://en.wikipedia.org/wiki/Open-mid_front_unrounded_vowel"
}
];
$audioIcon.click(() => $audioPlayer.play());
//#region Timer & modal window
var $modal = $("#modal");
var closeModal = $(".close");
function openModal(text) {
$("#modalText").innerText = text;
$modal.style.display = "block";
}
closeModal.onclick = function () {
$modal.style.display = "none";
};
window.onclick = function (event) {
if (event.target == modal) {
$modal.style.display = "none";
}
};
function countDown() {
let count = 600;
const timer = setInterval(function () {
count--;
$secondsDisplay.innerText = (count % 60).toString().length == 1 ? ('0' + (count % 60).toString()) : count % 60;
if (count % 60 == 0) {
$minutesDisplay.innerText = Math.floor(count / 60);
console.log(count / 60 + " minutes remained");
}
if (count === 60) {
openModal("1 minute left!");
}
if (count == 0) {
clearInterval(timer);
openModal("Time is over!");
setTimeout(() => document.location = String.Empty, 5000);
}
}, 1000);
}
//#endregion
function getRandomVowel() {
const randomIndex = Math.floor(Math.random() * vowels.length);
return vowels[randomIndex];
}
function checkAnswerHandle(currentVowel) {
const allAnswerBtns = $answersContainer.querySelectorAll(".answer");
const $checkedAnswerBtn = document.querySelector("#checked");
if (!$checkedAnswerBtn) {
$response.innerText = "Please select an answer!";
return;
}
$response.style.opacity = "1";
$response.removeAttribute('class');
$checkedAnswerBtn.removeAttribute("id");
if ($checkedAnswerBtn.textContent == currentVowel.vowel) {
$response.innerText = "Correct!";
$response.classList.add('correct');
score++;
} else {
$checkedAnswerBtn.classList.add("incorrect");
$response.innerHTML = `Incorrect! The correct answer was <a href="javascript:void(0)" onclick="getVowelLink('${currentVowel.link}')">${currentVowel.vowel}</a>.`;
$response.classList.add('incorrect');
userMistakeList.push({
correctVowel: currentVowel,
userMistake: vowels.find(vowel => vowel.vowel == $checkedAnswerBtn.textContent)
});
}
allAnswerBtns.forEach(button => {
if (button.textContent == currentVowel.vowel) {
button.classList.add("correct");
}
button.disabled = true;
button.style.pointerEvents = "none";
});
$scoreDisplay.innerHTML = score;
$checkAnswerBtn.disabled = true;
$checkAnswerBtn.style.display = "none";
$nextQuestionBtn.disabled = false;
$nextQuestionBtn.style.display = "block";
}
$checkAnswerBtn.addEventListener("click", function () {
checkAnswerHandle(currentVowel);
});
function getVowelLink(link) {
return window.open(link, '_blank', 'width=400px,height=600px,top=100px,left=500px');
}
function displayQuestion() {
if (currentQuestionIndex >= questionsCount) {
var $reloadQuizBtn = '<button type="button" class="button try-again" id="reloadQuiz"><i class="fa-solid fa-rotate-right"></i> Try again</button>';
$mainContainer.innerHTML = `<h1>Quiz Over!</h1><p>Your total score: <b>${score} / ${questionsCount}</b></p>${$reloadQuizBtn}`;
$mainContainer.classList.remove("main-container");
$mainContainer.classList.add("result-container");
var reloadQuizBtn = $("#reloadQuiz");
reloadQuizBtn.on("click", function () {
document.location = String.Empty;
});
return;
}
$checkAnswerBtn.disabled = false;
$checkAnswerBtn.style.display = "block";
$checkAnswerBtn.style.pointerEvents = "All";
if (userMistakeList.length > 0) {
if (userMistakeList[0].userMistake) {
currentVowel = userMistakeList[0].userMistake;
userMistakeList[0].userMistake = null;
} else if (userMistakeList[0].correctVowel) {
currentVowel = userMistakeList[0].correctVowel;
userMistakeList.shift();
}
} else {
currentVowel = getRandomVowel();
}
$audioPlayer.src = currentVowel.audio;
$audioPlayer.play();
$response.style.opacity = "0";
$answersContainer.innerHTML = String.Empty;
$nextQuestionBtn.disabled = true;
$nextQuestionBtn.style.display = "none";
let answers = [currentVowel];
while (answers.length < 4) {
let randomVowel = getRandomVowel();
if (!answers.some(v => v.vowel == randomVowel.vowel)) {
answers.push(randomVowel);
}
}
answers = swap(answers);
answers.forEach(vowel => {
let btn = document.createElement("button");
btn.classList.add("answer");
btn.textContent = vowel.vowel;
btn.click(function () {
document.querySelectorAll(".answer").forEach(button => button.removeAttribute("id"));
btn.id = "checked";
});
$answersContainer.appendChild(btn);
});
currentQuestionIndex++;
}
function startQuiz() {
$startPage.style.display = "none";
$quizContainer.style.display = "block";
countDown();
displayQuestion();
}
$("#startQuizBtn").click(startQuiz);
$nextQuestionBtn.click(displayQuestion);
</script>
</body>
</html>