-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
279 lines (249 loc) · 8.72 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
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
//start page global variables
var highScoreBtn = document.getElementById("high-score");
var beginQuizBtn = document.getElementById("begin-quiz");
var noStartBtn = document.getElementById("no-start");
//fun bonus button for start page
noStartBtn.addEventListener("click", function() {
alert('$ git commit -m "I guess come back when you are ready?"');
})
//questions object
var quizQuestions = [
{
question: "Which of the following is not one of the three fundamental programming languages of the modern web?",
choices: ["JavaScript", "C++", "HTML", "CSS"],
answer: "C++"
},
{
question: "A function tied to an object is called a ___.",
choices: ["String", "Boolean", "Web API", "Method"],
answer: "Method"
},
{
question: "The symbol ___ represents the ___ logical operator.",
choices: ["! ; NOT", "&& ; or", "=== ; equal to", ">= ; less than or equal"],
answer: "! ; NOT"
},
{
question: "HTML stands for _____.",
choices: ["Home Tool Markup Language", "Hyperlinks and Text Markup Language", "Hyper Text Markup Language", "Hacker Table Monitor Logout"],
answer: "Hyper Text Markup Language"
},
{
question: "What does WWW stand for?",
choices: ["Web World Wide", "World Wide Web", "Wide Web World", "Wireless Web Window"],
answer: "World Wide Web"
},
{
question: "Syntax in coding is _____.",
choices: ["The way you write the code.", "The coding language.", "An error that can appear in your code.", "Different languages mixed together."],
answer: "The way you write the code."
},
{
question: "How many columns make up Bootstrap's grid system?",
choices: ["10", "14", "16", "12"],
answer: "12"
}
]
//more global variables
var mainDiv = document.getElementById("main-div");
var quizDiv = document.getElementById("quiz-div");
var scoreDiv = document.getElementById("score-div");
var scoreText = document.getElementById("score-text");
var scoreList = document.getElementById("score-list-div");
var timer = document.getElementById("timer");
var timerEnd = document.getElementById("timer-end"); //false end of timer
var questionText = document.getElementById("question-text");
var answer1 = document.getElementById("answer1");
var answer2 = document.getElementById("answer2");
var answer3 = document.getElementById("answer3");
var answer4 = document.getElementById("answer4");
var i = 0;
var questionIndex = quizQuestions.length - 1;
var setTime = 60;
var score = 0;
var scoreInputName = document.getElementById("initials");
// console.log(questionIndex);
//begins the quiz on start, hides mainDiv and displays quizDiv
beginQuizBtn.addEventListener("click", function(event) {
mainDiv.className = "container-fluid m-3 d-none";
quizDiv.className = "container-fluid m-3";
timerFunction();
startQuiz ();
});
function timerFunction (duration, display) {
var time2 = duration, minutes, seconds
var quizTimer = setInterval (function () {
setTime--;
timer.innerHTML = setTime;
if (setTime === 0 ) {
clearInterval(quizTimer);
highScoreFunction();
}
}, 1000);
if (setTime === 0 || i === questionIndex) {
clearInterval(quizTimer);
}
};
//fills in the empty elements in the HTML and starts the timer
function startQuiz () {
var correctAnswer = quizQuestions[i];
questionText.textContent = quizQuestions[i].question;
answer1.innerHTML = correctAnswer.choices[0];
answer2.innerHTML = correctAnswer.choices[1];
answer3.innerHTML = correctAnswer.choices[2];
answer4.innerHTML = correctAnswer.choices[3];
};
//event bubbling of buttons with common class names
const answerBtns = document.getElementsByClassName("answer-btn");
for (const button of answerBtns) {
button.addEventListener("click", function(event) {
var correct = quizQuestions[i].answer;
// console.log(correct);
// if (i < questionIndex - 1) {
// i++;
// }
if (event.target.innerHTML == correct) {
i++;
score++;
startQuiz();
} else {
i++;
setTime -= 10;
startQuiz();
}
if (setTime === 0 || i >= questionIndex) {
highScoreFunction();
}
});
}
//event listeners on all buttons
// answer1.addEventListener("click", function (event) {
// event.stopPropagation();
// var correct = quizQuestions[i].answer;
// if (i < questionIndex) {
// i++;
// }
// if (event.target.innerHTML === correct) {
// i ++;
// score ++;
// startQuiz();
// } else {
// startQuiz();
// setTime -= 10;
// }
// // if (setTime === 0 || i >= questionIndex) {
// // highScoreFunction();
// // }
// });
// answer2.addEventListener("click", function (event) {
// event.stopPropagation();
// var correct = quizQuestions[i].answer;
// if (i < questionIndex) {
// i++;
// }
// if (event.target.innerHTML === correct) {
// i ++;
// score ++;
// startQuiz();
// } else {
// startQuiz();
// setTime -= 10;
// }
// // if (setTime === 0 || i >= questionIndex) {
// // highScoreFunction();
// // }
// });
// answer3.addEventListener("click", function (event) {
// event.stopPropagation();
// var correct = quizQuestions[i].answer;
// if (i < questionIndex) {
// i++;
// }
// if (event.target.innerHTML === correct) {
// i ++;
// score ++;
// startQuiz();
// } else {
// startQuiz();
// setTime -= 10;
// }
// // if (setTime === 0 || i >= questionIndex) {
// // highScoreFunction();
// // }
// });
// answer4.addEventListener("click", function (event) {
// event.stopPropagation();
// var correct = quizQuestions[i].answer;
// if (i < questionIndex) {
// i++;
// }
// if (event.target.innerHTML === correct) {
// i ++;
// score ++;
// startQuiz();
// } else {
// startQuiz();
// setTime -= 10;
// }
// // if (setTime === 0 || i >= questionIndex) {
// // highScoreFunction();
// // }
// });
function highScoreFunction() {
if (setTime === 0 || i >= questionIndex) {
timerEnd.className = "";
timer.className = "d-none";
quizDiv.className = "container-fluid m-3 d-none";
scoreDiv.className = "container-fluid m-3 px-5";
scoreList.className = "d-none";
scoreText.innerHTML = "Wow... You managed to successfully answer " + score + " questions right... I'm... impressed?";
}
}
var submitScoreBtn = document.getElementById("submitScoreBtn");
var scoreInputName = document.getElementById("initials");
var highScoreName = document.getElementById("user-initials");
var highScoreNum = document.getElementById("user-score");
submitScoreBtn.addEventListener("click", function (event) {
mainDiv.className = "container-fluid m-3 d-none";
quizDiv.className = "container-fluid m-3 d-none";
scoreDiv.className = "container-fluid m-3";
scoreList.className = "container-fluid m-3 d-none";
if (scoreInputName.value === "") {
alert("Don't be afraid to attach your name to this score...");
return false;
} else {
var savedScore = JSON.parse(localStorage.getItem("savedScore")) || [];
var currentInitials = scoreInputName.value.trim();
var currentScore = {
name: currentInitials,
score: score
};
savedScore.push(currentScore);
localStorage.setItem("savedScore", JSON.stringify(savedScore));
generateHighScores();
}
});
function generateHighScores() {
mainDiv.className = "container-fluid m-3 d-none";
quizDiv.className = "container-fluid m-3 d-none";
scoreDiv.className = "container-fluid m-3 d-none";
scoreList.className = "container-fluid m-3";
highScoreName.innerHTML = "";
highScoreNum.innerHTML = "";
var highscores = JSON.parse(localStorage.getItem("savedScore")) || [];
for (i = 0; i < highscores.length; i++) {
var newInitialLi = document.createElement("li");
var newScoreLi = document.createElement("li");
newInitialLi.textContent = highscores[i].name;
newScoreLi.textContent = highscores[i].score;
highScoreName.appendChild(newInitialLi);
highScoreNum.appendChild(newScoreLi);
}
};
highScoreBtn.addEventListener("click", function() {
mainDiv.className = "container-fluid m-3 d-none";
quizDiv.className = "container-fluid m-3 d-none";
scoreDiv.className = "d-none";
scoreList.className = "container-fluid m-3";
generateHighScores();
})