-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
259 lines (217 loc) · 9.04 KB
/
index.html
File metadata and controls
259 lines (217 loc) · 9.04 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="index.js"></script>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Josefin+Sans&display=swap" rel="stylesheet">
<title>Quiz</title>
</head>
<body class="bg-gradient-to-tr from-red-300 to-yellow-500 h-screen flex items-center justify-center">
<div class="bg-gray-300 p-5 rounded-2xl">
<div class="bg-gray-200 p-8 rounded-lg text-center">
<h1 class="text-6xl font-bold m-4" style="font-family: Bebas Neue, sans-serif;">Around the world in 10 questions</h1>
<form id="quiz-form">
<div id="front-page" class="front-page">
<h2 class="text-3xl p-2" style="font-family: Josefin Sans, sans-serif;">Click <span class="font-bold">START</span> to test your knowledge of the world</h2>
<button type="button" class="btn mt-6 bg-gray-800 text-white rounded-md py-2 px-3 text-3xl hover:bg-yellow-600" style="font-family: Bebas Neue, sans-serif; letter-spacing: .1rem;" id="start-button">START</button>
</div>
<div id="question-container" class="hidden">
<p class="text-3xl p-4" style="font-family: Josefin Sans;" id="question">Question goes here</p>
<div id="answer-buttons" class="flex flex-col text-2xl" style="font-family: Josefin Sans;">
<!-- Answer choices will be dynamically added here -->
</div>
<button type="button" class="btn mt-6 bg-gray-800 text-white rounded-md py-2 px-3 shadow-lg text-3xl hover:bg-yellow-600" style="font-family: Bebas Neue, sans-serif; letter-spacing: .1rem;" id="next-button">Next</button>
</div>
</form>
</div>
</div>
<script>
let questions = [
{
question: "In which country would you find the ancient city of Petra?",
answers: [
{ text: "Greece", correct: false },
{ text: "Jordan", correct: true },
{ text: "Egypt", correct: false },
{ text: "Turkey", correct: false }
]
},
{
question: "Which mountain range separates Europe from Asia?",
answers: [
{ text: "The Alps", correct: false },
{ text: "The Himalayas", correct: false },
{ text: "The Ural Mountains", correct: true },
{ text: "The Rocky Mountains", correct: false }
]
},
{
question: "What is the largest island in the Caribbean?",
answers: [
{ text: "Jamaica", correct: false },
{ text: "Puerto Rico", correct: false },
{ text: "Cuba", correct: true },
{ text: "Dominican Republic", correct: false }
]
},
{
question: "What is the capital city of Iceland?",
answers: [
{ text: "Reykjavik", correct: true },
{ text: "Oslo", correct: false },
{ text: "Helsinki", correct: false },
{ text: "Copenhagen", correct: false }
]
},
{
question: "Which African country is known as the 'Pearl of Africa'?",
answers: [
{ text: "Kenya", correct: false },
{ text: "Tanzania", correct: false },
{ text: "Uganda", correct: true },
{ text: "Ethiopia", correct: false }
]
},
{
question: "What is the currency of Japan?",
answers: [
{ text: "Won", correct: false },
{ text: "Yuan", correct: false },
{ text: "Yen", correct: true },
{ text: "Ringgit", correct: false }
]
},
{
question: "In which European city would you find the historic Acropolis?",
answers: [
{ text: "Rome", correct: false },
{ text: "Athens", correct: true },
{ text: "Paris", correct: false },
{ text: "Barcelona", correct: false }
]
},
{
question: "Which U.S. state is known as the 'Sunshine State'?",
answers: [
{ text: "California", correct: false },
{ text: "Florida", correct: true },
{ text: "Texas", correct: false },
{ text: "Arizona", correct: false }
]
},
{
question: "What is the official language of Brazil?",
answers: [
{ text: "Spanish", correct: false },
{ text: "Portuguese", correct: true },
{ text: "French", correct: false },
{ text: "Italian", correct: false }
]
},
{
question: "Which river is the longest in the world?",
answers: [
{ text: "Nile River", correct: true },
{ text: "Amazon River", correct: false },
{ text: "Yangtze River", correct: false },
{ text: "Mississippi River", correct: false }
]
},
];
const questionElement = document.getElementById("question");
const answerButtonContainer = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-button");
const quizForm = document.getElementById("quiz-form");
const frontPage = document.getElementById("front-page");
const questionContainer = document.getElementById("question-container");
let currentQuestionIndex = 0;
let score = 0;
let onFrontPage = true;
function startQuiz() {
if (onFrontPage) {
frontPage.classList.remove("hidden");
questionContainer.classList.add("hidden");
} else {
frontPage.classList.add("hidden");
questionContainer.classList.remove("hidden");
}
currentQuestionIndex = 0;
score = 0;
nextButton.innerText = "Next";
showQuestion();
}
function showQuestion() {
resetState();
let currentQuestion = questions[currentQuestionIndex];
let questionNo = currentQuestionIndex + 1;
questionElement.innerText = questionNo + ". " + currentQuestion.question;
currentQuestion.answers.forEach((answer, index) => {
const radioLabel = document.createElement("label");
radioLabel.classList.add("flex", "flex-center", "my-2", "mx-32", "border", "border-gray-500", "rounded", "p-2",
"hover:bg-yellow-600", "active:bg-yellow");
const radioInput = document.createElement("input");
radioInput.type = "radio";
radioInput.name = "answer";
radioInput.value = index;
radioInput.label = ""
radioInput.classList.add("mr-2");
const radioText = document.createElement("span");
radioText.innerText = answer.text;
radioLabel.appendChild(radioInput);
radioLabel.appendChild(radioText);
answerButtonContainer.appendChild(radioLabel);
});
}
//
function resetState() {
while (answerButtonContainer.firstChild) {
answerButtonContainer.removeChild(answerButtonContainer.firstChild);
}
}
function selectAnswer() {
// return the first input element with name="answer" that is checked and assign it to selectedRadio
const selectedRadio = document.querySelector('input[name="answer"]:checked');
if (selectedRadio) {
const selectedAnswerIndex = parseInt(selectedRadio.value);
const isCorrect = questions[currentQuestionIndex].answers[selectedAnswerIndex].correct;
if (isCorrect) {
score++;
}
}
}
function showScore() {
resetState();
questionElement.innerText = `You Scored ${score} out of ${questions.length}!`;
nextButton.innerText = "Play Again";
}
function handleNextButton() {
selectAnswer();
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
} else {
showScore();
}
}
nextButton.addEventListener("click", () => {
if (currentQuestionIndex < questions.length) {
handleNextButton();
} else {
startQuiz();
}
});
quizForm.addEventListener("submit", function (e) {
e.preventDefault();
handleNextButton();
});
document.getElementById("start-button").addEventListener("click", () => {
onFrontPage = false;
startQuiz();
});
</script>
</body>
</html>