-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
666 lines (607 loc) · 18.6 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta
name="description"
content="QuizTime - Test your knowledge with skill-testing questions."
/>
<meta name="keywords" content="quiz, trivia, quiz app" />
<title>QuizTime</title>
<style>
/* body styles */
body {
font-family: "Helvetica Neue", Arial, sans-serif;
background-color: hsl(192, 17%, 94%);
background-image: url(https://res.cloudinary.com/ceipt-geek/image/upload/v1725207662/background_hcmcun.avif);
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
min-height: 100vh;
}
/* logo styles */
.logo {
height: 12rem;
max-width: 100%;
margin-top: 2rem;
margin-bottom: 3rem;
display: block;
margin-left: auto;
margin-right: auto;
}
/* container styles */
.main-container {
background-color: hsl(0, 0%, 100%);
border-radius: 15px;
padding: 25px;
max-width: 500px;
width: 90%;
box-shadow: 0 4px 15px hsla(0, 0%, 0%, 0.1);
display: flex;
flex-direction: column;
gap: 1rem;
margin-top: 2rem;
margin-bottom: 1rem;
}
.intro-container,
.question-container,
.result-container {
text-align: center;
}
/* Intro section */
.intro-container {
display: flex;
flex-direction: column;
align-items: center;
}
.intro-container h1 {
font-size: 2rem;
margin-bottom: 0.5rem;
}
.intro-container p {
font-size: 1rem;
margin-bottom: 2rem;
}
/* Result section */
.result-container {
display: none;
}
.result-container p {
font-size: 1.4rem;
}
/* Question section */
.question-container {
display: none;
}
.question {
font-size: 1.5rem;
color: hsl(0, 0%, 20%);
font-weight: bold;
text-align: center;
margin-bottom: 1rem;
}
.options {
display: flex;
flex-direction: column;
gap: 15px;
}
.option {
padding: 12px;
border: 2px solid hsl(200, 100%, 93%);
border-radius: 8px;
cursor: pointer;
background-color: hsla(200, 50%, 90%, 0.3);
transition: background-color 0.3s, border-color 0.3s;
font-size: 1.1rem;
text-align: center;
}
.option:hover:not(.disabled) {
background-color: hsl(202, 100%, 93%);
}
.option.selected {
border-color: hsl(122, 39%, 49%);
background-color: hsl(120, 100%, 94%);
}
.option.correct {
border-color: hsl(122, 39%, 49%);
background-color: hsl(120, 100%, 94%);
}
.option.incorrect {
border-color: hsl(4, 90%, 58%);
background-color: hsl(0, 100%, 94%);
}
.navigation {
display: flex;
justify-content: space-between;
margin-top: 1rem;
}
/* Button styles */
button {
padding: 12px 25px;
background-color: hsl(204, 80%, 49%);
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 1rem;
transition: background-color 0.3s, box-shadow 0.3s;
}
button:disabled {
background-color: hsl(0, 0%, 80%);
cursor: not-allowed;
}
button:hover:not(:disabled) {
background-color: hsl(204, 80%, 45%);
box-shadow: 0 4px 10px hsla(0, 0%, 0%, 0.1);
}
button:active:not(:disabled) {
background-color: hsl(204, 80%, 40%);
box-shadow: 0 4px 10px hsla(0, 0%, 0%, 0.1);
transform: scale(0.95);
}
/* tablet */
@media (max-width: 800px) {
.logo {
height: auto;
max-width: 60%;
}
.main-container {
max-width: 85%;
margin-top: 0.5rem;
margin-bottom: 1rem;
}
.intro-container h1 {
font-size: 1.7rem;
}
.intro-container p {
font-size: 0.9rem;
margin-bottom: 1.5rem;
}
.question {
font-size: 1.2rem;
}
.options {
gap: 12px;
}
.option {
padding: 10px;
font-size: 1rem;
}
.result-container p {
font-size: 1.2rem;
}
button {
font-size: 0.8rem;
padding: 10px 20px;
}
}
/* mobile */
@media (max-width: 600px) {
.main-container {
padding: 20px;
margin-top: 1rem;
}
.intro-container h1 {
font-size: 1.5rem;
}
.intro-container p {
font-size: 0.8rem;
margin-bottom: 1rem;
}
.question {
font-size: 1rem;
}
.options {
gap: 10px;
}
.option {
padding: 8px;
font-size: 0.8rem;
}
.result-container p {
font-size: 1rem;
}
button {
font-size: 0.7rem;
padding: 8px 15px;
}
}
</style>
</head>
<body>
<header role="banner">
<img
class="logo"
src="https://res.cloudinary.com/ceipt-geek/image/upload/v1725207655/no-bg_cbpker.png"
alt="QuizTime Logo"
/>
</header>
<main class="main-container" role="main">
<section
id="intro-container"
class="intro-container"
aria-labelledby="intro-heading"
>
<h1 id="intro-heading">Welcome to QuizTime!</h1>
<p>
Test your knowledge with 4 carefully curated, skill-testing questions.
</p>
<button
id="start-btn"
aria-controls="quiz-container"
aria-expanded="false"
>
Start Quiz
</button>
</section>
<section
id="quiz-container"
class="question-container"
aria-labelledby="question-heading"
aria-hidden="true"
>
<div
id="question-container"
class="question"
role="heading"
aria-level="2"
>
<!-- Question will appear here -->
</div>
<div
id="options-container"
class="options"
role="radiogroup"
aria-labelledby="question-container"
>
<!-- Options will appear here -->
</div>
<div class="navigation" role="navigation" aria-label="Quiz navigation">
<button id="prev-btn" disabled aria-controls="question-container">
Previous
</button>
<button id="next-btn" aria-controls="question-container">Next</button>
</div>
</section>
<section
id="result-container"
class="result-container"
aria-labelledby="result-heading"
aria-hidden="true"
>
<h2 id="result-heading">Your Results</h2>
<p role="alert">
<!-- Final score will be displayed here -->
</p>
</section>
</main>
<script>
// quiz questions
const quizData = [
{
question: "What is the capital of France?",
options: ["Berlin", "Madrid", "Paris", "Rome"],
correct: 2,
},
{
question: "Who wrote 'To Kill a Mockingbird'?",
options: [
"Harper Lee",
"Jane Austen",
"Mark Twain",
"Charles Dickens",
],
correct: 0,
},
{
question: "What is the smallest planet in our solar system?",
options: ["Mars", "Earth", "Mercury", "Venus"],
correct: 2,
},
{
question: "Which element has the chemical symbol 'O'?",
options: ["Oxygen", "Osmium", "Oganesson", "Oganesson"],
correct: 0,
},
{
question: "Who painted the Mona Lisa?",
options: [
"Vincent van Gogh",
"Pablo Picasso",
"Leonardo da Vinci",
"Claude Monet",
],
correct: 2,
},
{
question: "What is the hardest natural substance on Earth?",
options: ["Gold", "Iron", "Diamond", "Platinum"],
correct: 2,
},
{
question: "Which planet is known as the Red Planet?",
options: ["Mars", "Venus", "Jupiter", "Saturn"],
correct: 0,
},
{
question: "Who discovered penicillin?",
options: [
"Marie Curie",
"Alexander Fleming",
"Isaac Newton",
"Albert Einstein",
],
correct: 1,
},
{
question: "What is the capital city of Australia?",
options: ["Sydney", "Melbourne", "Canberra", "Brisbane"],
correct: 2,
},
{
question: "Which country hosted the 2016 Summer Olympics?",
options: ["China", "Brazil", "Greece", "Russia"],
correct: 1,
},
{
question: "In which year did the Titanic sink?",
options: ["1905", "1912", "1918", "1923"],
correct: 1,
},
{
question: "Which gas is most abundant in Earth's atmosphere?",
options: ["Oxygen", "Carbon Dioxide", "Nitrogen", "Hydrogen"],
correct: 2,
},
{
question: "Who invented the telephone?",
options: [
"Thomas Edison",
"Alexander Graham Bell",
"Nikola Tesla",
"Guglielmo Marconi",
],
correct: 1,
},
{
question: "What is the tallest mountain in the world?",
options: ["K2", "Kangchenjunga", "Lhotse", "Mount Everest"],
correct: 3,
},
{
question: "Which planet is closest to the Sun?",
options: ["Mercury", "Venus", "Earth", "Mars"],
correct: 0,
},
{
question: "Which is the largest ocean on Earth?",
options: [
"Atlantic Ocean",
"Indian Ocean",
"Arctic Ocean",
"Pacific Ocean",
],
correct: 3,
},
{
question: "Who was the first man to walk on the Moon?",
options: [
"Yuri Gagarin",
"Buzz Aldrin",
"Neil Armstrong",
"Michael Collins",
],
correct: 2,
},
{
question:
"Which organ in the human body is primarily responsible for filtering blood?",
options: ["Liver", "Lungs", "Heart", "Kidneys"],
correct: 3,
},
{
question: "What is the chemical formula for water?",
options: ["H2O", "O2", "CO2", "H2O2"],
correct: 0,
},
{
question:
"Which Shakespeare play features the characters Rosencrantz and Guildenstern?",
options: ["Macbeth", "Hamlet", "Othello", "King Lear"],
correct: 1,
},
{
question: "Who is the author of the Harry Potter series?",
options: [
"J.K. Rowling",
"J.R.R. Tolkien",
"George R.R. Martin",
"Stephen King",
],
correct: 0,
},
{
question: "Which country is the largest by land area?",
options: ["Canada", "China", "Russia", "United States"],
correct: 2,
},
{
question: "What is the square root of 144?",
options: ["10", "12", "14", "16"],
correct: 1,
},
{
question: "Which is the largest planet in our solar system?",
options: ["Saturn", "Neptune", "Jupiter", "Uranus"],
correct: 2,
},
{
question: "In which year did World War II end?",
options: ["1941", "1943", "1945", "1947"],
correct: 2,
},
{
question: "Which element is known as the 'King of Chemicals'?",
options: [
"Sulfuric Acid",
"Hydrochloric Acid",
"Nitric Acid",
"Sodium Hydroxide",
],
correct: 0,
},
{
question: "Which company is known for the slogan 'Just Do It'?",
options: ["Adidas", "Nike", "Puma", "Reebok"],
correct: 1,
},
{
question: "What is the most spoken language in the world?",
options: ["Spanish", "English", "Hindi", "Mandarin"],
correct: 3,
},
{
question: "What is the main ingredient in traditional Japanese sake?",
options: ["Rice", "Wheat", "Barley", "Corn"],
correct: 0,
},
{
question: "Which planet is known as the Earth's twin?",
options: ["Mars", "Venus", "Jupiter", "Saturn"],
correct: 1,
},
];
// variables
let currentQuestionIndex = 0;
let score = 0;
let selectedQuestions = [];
let userAnswers = {};
// containers
const questionContainer = document.getElementById("question-container");
const optionsContainer = document.getElementById("options-container");
const resultContainer = document.getElementById("result-container");
const introContainer = document.getElementById("intro-container");
const quizContainer = document.getElementById("quiz-container");
//buttons
const prevBtn = document.getElementById("prev-btn");
const nextBtn = document.getElementById("next-btn");
const startBtn = document.getElementById("start-btn");
// initiate scripts on start button click
startBtn.onclick = () => startQuiz();
// entry point. Hides intro container, shows quiz container. Gets 4 random questions from quizData array
// resets variables, and loads first question
function startQuiz() {
introContainer.style.display = "none";
quizContainer.style.display = "block";
selectedQuestions = getRandomQuestions();
score = 0;
userAnswers = {};
loadQuestion();
}
// shuffle quizData array and take first 4 questions
function getRandomQuestions() {
// note: Math.random() = range(0, 1). sort compares positive and negative values to determine order
// 0.5 - Math.random() = range(-0.5, 0.5)
const shuffled = [...quizData].sort(() => 0.5 - Math.random());
return shuffled.slice(0, 4);
}
// generate question and options to display on screen
function loadQuestion() {
const currentQuestion = selectedQuestions[currentQuestionIndex];
questionContainer.innerText = currentQuestion.question;
optionsContainer.innerHTML = "";
currentQuestion.options.forEach((option, index) => {
const optionElement = document.createElement("div");
optionElement.classList.add("option");
optionElement.textContent = option;
// If question previously answered, highlight user selected option and/or correct option
// and disable the option
if (userAnswers[currentQuestionIndex] !== undefined) {
if (index === userAnswers[currentQuestionIndex].selectedIndex) {
optionElement.classList.add(
userAnswers[currentQuestionIndex].isCorrect
? "correct"
: "incorrect"
);
} else if (currentQuestion.correct === index) {
optionElement.classList.add("correct");
}
optionElement.classList.add("disabled");
} else {
// otherwise, add an onClick event to the option
optionElement.onclick = () => selectOption(index, optionElement);
}
optionsContainer.appendChild(optionElement);
});
updateNavigationButtons();
}
// when user clicks an option, check if it's correct. Highlight correct option and incorrect selected option accordingly
// add user's answer to userAnswers object, disable all options and remove onclick event listeners
// update score internally
function selectOption(index, optionElement) {
const currentQuestion = selectedQuestions[currentQuestionIndex];
const isCorrect = index === currentQuestion.correct;
userAnswers[currentQuestionIndex] = {
selectedIndex: index,
isCorrect: isCorrect,
};
optionElement.classList.add(isCorrect ? "correct" : "incorrect");
if (!isCorrect)
document
.querySelectorAll(".option")
[currentQuestion.correct].classList.add("correct");
if (isCorrect) score += 1;
document.querySelectorAll(".option").forEach((option) => {
option.classList.add("disabled");
option.onclick = null;
});
}
// disable/enable prevButton if user is on first question. Change nextBtn text to submit if on last question
function updateNavigationButtons() {
prevBtn.disabled = currentQuestionIndex === 0;
nextBtn.textContent =
currentQuestionIndex === selectedQuestions.length - 1
? "Submit"
: "Next";
}
// when submit button clicked, replaces quiz container with result container. Ask user if they want to restart quiz
function showResult() {
quizContainer.style.display = "none";
resultContainer.style.display = "block";
resultContainer.innerHTML = `
<p>Your score: ${score}/${selectedQuestions.length}</p>
<button onclick="retakeQuiz()">Retake Quiz</button>
`;
}
// reset state of quiz. take user back to intro container
function retakeQuiz() {
currentQuestionIndex = 0;
score = 0;
selectedQuestions = [];
resultContainer.style.display = "none";
introContainer.style.display = "block";
}
// traverse question array backward and reload previous question
prevBtn.onclick = () => {
if (currentQuestionIndex > 0) {
currentQuestionIndex--;
loadQuestion();
}
};
// traverse question array forwrd and load next question
// if on less question, call showResult which hides the quiz container and shoes the result container
nextBtn.onclick = () => {
if (currentQuestionIndex < selectedQuestions.length - 1) {
currentQuestionIndex++;
loadQuestion();
} else {
showResult();
}
};
</script>
</body>
</html>