-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
291 lines (200 loc) · 8.38 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quiz App (Multilingual)</title>
<link rel="stylesheet" href="style.css">
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/lipis/flag-icons@6.9.2/css/flag-icons.min.css"
/>
</head>
<body>
<div class="container">
<div class="heading">Quiz App</div>
<details class="language-selection" id="language-selection">
<summary style="cursor:pointer;">Language</summary>
<span onclick="translatePage('en')"><span class="fi fi-gb"></span> English</span>
<span onclick="translatePage('tr')"><span class="fi fi-tr"></span> Turkish</span>
<span onclick="translatePage('de')"><span class="fi fi-de"></span> German</span>
</details>
<div id="start-container">
<p class="quiz-app-description">
Welcome to my multilingual quiz app. Select your language and test your knowledge!
</p>
<button onclick="loadQuestions()" type="button" class="button button-secondary">Start Game</button>
</div>
<div class="question-container" id="question-container">
<div class="points-container">
<img src="gold-medal-icon.svg" alt="Points" height="30px">
<span id="points-count">0</span>
</div>
<div class="question">Question <span id="question-counter">1</span>:</div>
<div class="question-text" id="question-text">What is the capital of France?</div>
<div class="choices" id="choices">
<label class="choice"><input type="radio" name="question" value="paris">Paris</label>
<label class="choice"><input type="radio" name="question" value="london">London</label>
<label class="choice"><input type="radio" name="question" value="berlin">Berlin</label>
</div>
<button class="submit-button" type="button" onclick="findSelection()">Submit Answer</button>
</div>
<div id="result-container" style="display: none;">
<p class="quiz-app-description">
Quiz completed! Here is the result of your knowledge 😉
</p>
<div id="progressbar" aria-valuemin="0" aria-valuemax="100" style="--value:10"></div>
<details class="progress-details" id="progress-details">
<summary style="cursor:pointer">View Details</summary>
<!-- here are the details injected with js -->
</details>
<button onclick="location.reload()" type="button" class="button button-secondary">Play Again</button>
</div>
</div>
<script>
categories = [27, 10, 15];
points = 0;
currentQuestion = -1;
questionsAmount = 10;
userAnswers = [];
questions = [];
function goToNextQuestion(){
if (currentQuestion < ( questionsAmount - 1) ) {
currentQuestion++;
document.getElementById("question-counter").innerText = `${currentQuestion + 1}/${questionsAmount}`;
document.getElementById("question-text").innerHTML = questions[currentQuestion]["question"];
allAnswers = questions[currentQuestion]["incorrect_answers"] ;
allAnswers.push(questions[currentQuestion]["correct_answer"])
shuffleArray(allAnswers);
choicesHtml = "";
for(var i = 0, length = allAnswers.length; i < length; i++){
choicesHtml += `<label class="choice"><input type="radio" name="question" value="${i}">${allAnswers[i]}</label>`;
}
document.getElementById("choices").innerHTML = choicesHtml;
}
else{
showResult();
}
}
function showResult(){
document.getElementById("question-container").style.display = "none";
document.getElementById("result-container").style.display = "block";
//document.getElementById("language-selection").style.display = "flex";
document.getElementById("progressbar").style=`--value:${(points/questionsAmount)*100}`;
progressDetailsHtml = "";
progressDetailsHtml += `<br><span style="color:#009933">✔ Correct: ${points}/${questionsAmount}</span><br>`;
progressDetailsHtml += `<span style="color:red">✖ Wrong: ${questionsAmount - points}/${questionsAmount}</span><br><br>`;
for(var i = 0, length = questions.length; i < length; i++){
questionToAdd = questions[i]
questionText = questionToAdd["question"];
questionCorrectAnswer = questionToAdd["correct_answer"];
questionUserAnswer = userAnswers[i];
answerColor = "";
if(questionCorrectAnswer === questionUserAnswer){
answerColor = "#009933";
}
else{
answerColor = "red";
}
progressDetailsHtml += `
<span>
<strong>Question ${i +1}:</strong> ${questionText}<br>
You answered: <span style="color:${answerColor}"><strong>${questionUserAnswer}</strong></span><br>
The correct answer was: <strong>${questionCorrectAnswer}</strong><br><br>
</span>`;
}
document.getElementById("progress-details").innerHTML += progressDetailsHtml;
}
function checkAnswer(userAnswer){
if( userAnswer === questions[currentQuestion]["correct_answer"] ){
points++;
document.getElementById("points-count").style.color="#009933";
}
else{
document.getElementById("points-count").style.color="red";
}
userAnswers.push(userAnswer);
document.getElementById("points-count").innerText = points;
startShakeAnimation();
}
function startShakeAnimation() {
const pointsCountElement = document.getElementById("points-count");
pointsCountElement.classList.add("shake-animation");
// Remove the shake class after the animation completes
pointsCountElement.addEventListener("animationend", () => {
pointsCountElement.classList.remove("shake-animation");
});
}
/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
};
}
function loadQuestions(){
// URL of the external JSON file
const jsonURL = "https://opentdb.com/api.php?amount=10&category=27&difficulty=easy&type=multiple";
// Fetch JSON data from the external URL
fetch(jsonURL)
.then(response => {
// Check if the request was successful (status code 200)
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
})
.then(data => {
// Display JSON data on the webpage
console.log("fetch succesful");
questions = data["results"];
document.getElementById("start-container").style.display = "none";
document.getElementById("language-selection").style.display = "none";
document.getElementById("question-container").style.display = "block";
goToNextQuestion();
})
.catch(error => {
// Handle any errors that occurred during the fetch
console.error("Error fetching data:", error);
showLoadAgain();
});
}
function findSelection(){
var radios = document.getElementsByName('question');
for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
// do whatever you want with the checked radio
checkAnswer(radios[i].parentElement.innerText);
goToNextQuestion();
// only one radio can be logically checked, don't check the rest
break;
}
}
}
function showLoadAgain(){
document.getElementById("start-container").innerHTML += `<i style="color:red;">Error. Couldn't load data. Try again in a moment.</i><br>`;
}
function translatePage(languageName){
window.location = `#googtrans(en|${languageName})`;
location.reload();
setTimeout(hideGoogleBar, 3000);
}
function hideGoogleBar(){
// hide ugly google translate top bar
document.getElementsByTagName("iframe")[0].style.visibility = "hidden";
alert("hi");
}
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en'
},
'google_translate_element'
);
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
</body>
</html>