-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquiz.js
151 lines (135 loc) · 4.55 KB
/
quiz.js
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
const questions = [
{
question: "When is Hyojoo's birthday?",
answers: [
{ text: "8/2", correct: true },
{ text: "8/4", correct: false },
{ text: "8/22", correct: false },
{ text: "8/24", correct: false }
]
},
{
question: "What is Hyojoo's favorite sport?",
answers: [
{ text: "Badminton", correct: false },
{ text: "Bowling", correct: true },
{ text: "Swimming", correct: false },
{ text: "Tennis", correct: false }
]
},
{
question: "What is Hyojoo's blood type?",
answers: [
{ text: "A", correct: true },
{ text: "B", correct: false },
{ text: "O", correct: false },
{ text: "AB", correct: false }
]
},
{
question: "What is Hyojoo's favorite season?",
answers: [
{ text: "Spring", correct: true },
{ text: "Summer", correct: false },
{ text: "Fall", correct: false },
{ text: "Winter", correct: false }
]
},
{
question: "What has Hyojoo never experienced before?",
answers: [
{ text: "Falling into a sinkhole", correct: false },
{ text: "1 million won for the hospital fee", correct: false },
{ text: "Get hit by her brother", correct: false },
{ text: "A wedding celebration performance", correct: true }
]
},
{
question: "What wasn't Hyojoo's childhood dream?",
answers: [
{ text: "Police", correct: false },
{ text: "Ballerina", correct: false },
{ text: "Doctor", correct: true },
{ text: "Teacher", correct: false }
]
}
];
const questionElement = document.getElementById('question');
const answerButtonsElement = document.getElementById('answer-buttons');
const nextButton = document.getElementById('next-btn');
const scoreContainer = document.getElementById('score-container');
const scoreElement = document.getElementById('score');
const resultElement = document.getElementById('result');
let currentQuestionIndex = 0;
let score = 0;
let selectedButton = null;
function startQuiz() {
currentQuestionIndex = 0;
score = 0;
nextButton.classList.add('hide');
scoreContainer.classList.add('hide');
questionElement.classList.remove('hide');
resultElement.classList.add('hide');
showQuestion(questions[currentQuestionIndex]);
}
function showQuestion(question) {
questionElement.innerText = question.question;
answerButtonsElement.innerHTML = '';
resultElement.classList.add('hide');
question.answers.forEach(answer => {
const button = document.createElement('button');
button.innerText = answer.text;
button.classList.add('btn');
button.dataset.correct = answer.correct;
button.addEventListener('click', selectAnswer);
answerButtonsElement.appendChild(button);
});
}
function selectAnswer(e) {
const clickedButton = e.target;
const correct = clickedButton.dataset.correct === 'true';
highlightSelectedButton(clickedButton);
if (correct) {
score += 100;
resultElement.innerText = 'Correct!';
} else {
resultElement.innerText = 'Wrong!';
}
resultElement.classList.remove('hide');
Array.from(answerButtonsElement.children).forEach(button => {
setStatusClass(button, button.dataset.correct === 'true');
});
if (questions.length > currentQuestionIndex + 1) {
nextButton.classList.remove('hide');
} else {
showScore();
}
}
function highlightSelectedButton(button) {
if (selectedButton) {
selectedButton.classList.remove('selected');
}
button.classList.add('selected');
selectedButton = button;
}
function setStatusClass(element, correct) {
element.classList.toggle('correct', correct);
element.classList.toggle('wrong', !correct);
}
function showScore() {
document.getElementById("question-container").classList.add('hide');
nextButton.classList.add('hide');
scoreContainer.classList.remove('hide');
scoreElement.innerText = `${score} / 600`;
if (score === 600) {
resultElement.innerText = 'Perfect Score!';
resultElement.classList.remove('hide');
}
}
nextButton.addEventListener('click', () => {
nextButton.classList.add('hide');
currentQuestionIndex++;
showQuestion(questions[currentQuestionIndex]);
selectedButton = null;
});
document.addEventListener('DOMContentLoaded', startQuiz);