-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
194 lines (182 loc) · 5.69 KB
/
script.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
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
const questions = [
{
question: "Where did we meet each other?",
answers: [
{text: "Rave", correct: false},
{text: "Arizona State Fair", correct: false},
{text: "Buffalo Wild Wings", correct: true},
{text: "Ross", correct: false},
]
},
{
question: "Where was our first date?",
answers: [
{text: "McDonald's", correct: false},
{text: "Nandos", correct: false},
{text: "Bowling", correct: false},
{text: "Mellow Mushroom", correct: true},
]
},
{
question: "Who said the L word First?",
answers: [
{text: "Alec", correct: true},
{text: "Alli", correct: false},
]
},
{
question: "Who kissed the other one first?",
answers: [
{text: "Alli", correct: true},
{text: "Alec", correct: false},
]
},
{
question: "What was the first gift given in our relationship?",
answers: [
{text: "Donuts", correct: false},
{text: "Flowers", correct: false},
{text: "Crispy M&M's", correct: true},
{text: "Cookies", correct: false},
]
},
{
question: "When did we officially start dating?",
answers: [
{text: "12/30/22", correct: false},
{text: "01/01/23", correct: false},
{text: "12/31/22", correct: true},
{text: "11/29/22", correct: false},
]
},
{
question: "Who was the first person to text?",
answers: [
{text: "Alec", correct: false},
{text: "Alli", correct: true},
]
},
{
question: "First out of state vacation?",
answers: [
{text: "EDCLV", correct: false},
{text: "Beyond Wonder Land", correct: true},
{text: "Colorado", correct: false},
{text: "Escape", correct: false},
]
},
{
question: "First music event together?",
answers: [
{text: "Beyond Wonder Land", correct: false},
{text: "Phoenix Lights", correct: false},
{text: "Decadence", correct: false},
{text: "Raggaeton", correct: true},
]
},
{
question: "When was our first kiss?",
answers: [
{text: "October 3rd 2022", correct: true},
{text: "October 23rd 2022", correct: false},
{text: "November 6th 2022", correct: false},
{text: "December 25th 2022", correct: false},
]
},
{
question: "What was the first holiday we spent together?",
answers: [
{text: "Christmas", correct: false},
{text: "Thankgiving", correct: false},
{text: "Halloween", correct: true},
{text: "New Years", correct: false},
]
},
{
question: "Where did we hold hands for the first time?",
answers: [
{text: "Outside Buffalo", correct: false},
{text: "Mellow Mushroom", correct: false},
{text: "Halloween party", correct: false},
{text: "Dave & Buster's", correct: true},
]
}
];
const questionElement = document.getElementById("question");
const answerButtons = document.getElementById("answer-buttons");
const nextButton = document.getElementById("next-btn");
let currentQuestionIndex = 0;
let score = 0;
function startQuiz() {
currentQuestionIndex = 0;
score = 0;
nextButton.innerHTML = "Next";
showQuestion();
}
function showQuestion() {
resetState();
let currentQuestion = questions[currentQuestionIndex];
let questionNo = currentQuestionIndex + 1;
questionElement.innerHTML = questionNo + ". " + currentQuestion.question;
currentQuestion.answers.forEach(answer => {
const button = document.createElement("button");
button.innerHTML = answer.text;
button.classList.add("btn");
answerButtons.appendChild(button);
if (answer.correct) {
button.dataset.correct = answer.correct;
}
button.addEventListener("click", selectAnswer);
});
}
function resetState() {
nextButton.style.display = "none";
while (answerButtons.firstChild) {
answerButtons.removeChild(answerButtons.firstChild);
}
}
function selectAnswer(e){
const selectedBtn = e.target;
const isCorrect = selectedBtn.dataset.correct === "true";
if (isCorrect) {
selectedBtn.classList.add("correct");
score++;
}else{
selectedBtn.classList.add("incorrect");
}
Array.from(answerButtons.children).forEach(button => {
if(button.dataset.correct === "true"){
button.classList.add("correct");
}
button.disabled = true;
});
nextButton.style.display = "block";
}
function showScore(){
resetState();
if ( score == questions.length) {
questionElement.innerHTML = "Good Job! You got all of them correct! Here is your prize:";
var video = document.getElementById("video");
video.style.display = "block";
}else(
questionElement.innerHTML = `You scored ${score} out of ${questions.length}! Get all of the questions correct for a prize`
)
nextButton.innerHTML = "Play Again";
nextButton.style.display = "block";
}
function handleNextQuestion() {
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
showQuestion();
}else{
showScore();
}
}
nextButton.addEventListener("click", () => {
if (currentQuestionIndex < questions.length) {
handleNextQuestion();
}else{
startQuiz();
}
});
startQuiz();