-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
250 lines (206 loc) · 6.96 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
const quiz = {
questions: [
{
question: 'Which of the following is not JavaScript Data Types?',
choices: ['Undefined', 'Number', 'Boolean', 'Float'],
answer: 'Float'
},
{
question: 'What are variables used for in JavaScript Programs?',
choices: ['Storing numbers, dates, or other values', 'Varying randomly','Causing high-school algebra flashbacks', 'None of the above'],
answer: 'Storing numbers, dates, or other values'
},
{
question: 'Inside which HTML element do we put the JavaScript?',
choices: ['<script>', '<head>', '<meta>', '<style>'],
answer: '<script>'
}
],
timeLeft: 75,
currentQuestion: 0,
score: 0,
incrementCurrentQuestion() {
this.currentQuestion++;
},
isChoiceCorrect(choice) {
let currentQuestionIndex = this.currentQuestion;
if (choice === this.questions[currentQuestionIndex].answer) {
return true;
} else {
return false;
}
},
incrementScore() {
this.score += 10;
},
decrementScore() {
this.score -= 10;
},
decrementTimer() {
this.timeLeft -= 10;
},
setTime() {
this.timeLeft--;
}
}
const mainEl = $('#main-content');
const answerResponseEl = $('#answer-response');
const timeLeftEl = $('#time-left');
const viewHighScoresBtn = $('#view-highscores');
const displayWelcomeMsg = function() {
mainEl.append(
`<div class="text-center">
<h2>Coding Quiz Challenge</h2>
<p id="instructions">
Try to answer the following code related questions within
the time limit. Keep in mind that incorrect answers will penalize
score/time by ten seconds!
</p>
<button id="btn-start" class="btn btn-primary btn-purple">Start Quiz</button>
</div>`);
}
// Displays the time left
const displayTimeLeft = function(timeLeft) {
timeLeftEl.text(timeLeft);
}
displayWelcomeMsg();
displayTimeLeft(quiz.timeLeft);
// Displays the current question and choices to the main el.
const displayQuestion = function(quiz) {
let questionIndex = quiz.currentQuestion;
let question = quiz.questions[questionIndex];
console.log(question);
// append question to main content
mainEl.append(`<h2>${question.question}</h2>`);
// creat the list for answer choices
let listEl = $('<ol id="choices">');
// loop through choices and append items to the list
for (var i = 0; i < question.choices.length; i++) {
let choice = question.choices[i];
listEl.append(`<li><button class="choice-btn btn-purple">${choice}</button></li>`);
}
// append list to mainEl
mainEl.append(listEl);
}
// Displays final scores and initials form.
const displayFinalMsgForm = function() {
mainEl.append(`
<h2>All done!</h2
<p>Final score: ${quiz.score}</p>
<form id="initials-form">
<label for="initials">Your initials:</label>
<input type="text" id="initials" name="initials">
<input class="btn btn-primary btn-purple" type="submit" value="Submit">
</form>`
);
}
// Displays high scores
const displayHighScores = function() {
mainEl.html('');
mainEl.append(`<h2>High Scores</h2>`);
let highScores = localStorage.getItem("highScores");
if (!highScores || highScores.length === 0) {
highScores = [];
} else {
highScores = JSON.parse(highScores);
}
let scoresListEl = $('<ol id="scores-list">');
// loop through high scores and add to the list
for (var i = 0; i < highScores.length; i++) {
scoresListEl.append(`<li>${highScores[i].initials} ${highScores[i].score}</li>`);
}
mainEl.append(scoresListEl);
mainEl.append(`<div>
<button id="go-back" class="btn btn-primary btn-purple">Go Back</button>
<button id="clear-highscores" class="btn btn-primary btn-purple">Clear Highscores</button></div>
`);
}
const handleGoBack = function() {
location.reload();
}
const handleClearHighScores = function() {
localStorage.setItem("highScores", []);
displayHighScores();
}
// Updates initials and score from localstorage
const handleInitialsForm = function(event) {
event.preventDefault();
console.log('Initials form clicked!');
const initialsInput = $('#initials');
let highScores = localStorage.getItem("highScores");
if (!highScores || highScores.length === 0) {
highScores = [];
} else {
highScores = JSON.parse(highScores);
}
let highScore = {
initials: initialsInput.val(),
score: quiz.score
}
highScores.push(highScore);
localStorage.setItem("highScores", JSON.stringify(highScores));
// clears main el
mainEl.html(' ')
displayHighScores();
}
// Start quiz button event listener
$('#btn-start').on('click', function() {
setTimer();
// clears main el
mainEl.html('');
displayQuestion(quiz);
});
const setTimer = function() {
const timer = setInterval(function(){
// Decrements timeLeft by 1
quiz.setTime();
var timeLeft = quiz.timeLeft;
displayTimeLeft(timeLeft);
if (timeLeft < 0) {
clearInterval(timer);
// clears main el
mainEl.html('');
displayTimeLeft(0);
displayFinalMsgForm();
}
}, 1000);
}
// Evaluates the user's answer choice, updates the score/timer, and checks if there are any more questions.
// If the quiz is over, it displays the final message form.
const handleChoiceClicks = function(event) {
// Evaluate answer and update score
let choice = $(event.target).text();
if (quiz.isChoiceCorrect(choice)) {
displayAnswerResponse('Correct!');
console.log('Correct!');
quiz.incrementScore();
console.log(`Curr score: ${quiz.score}`);
} else {
displayAnswerResponse('Wrong!');
console.log('Wrong!');
quiz.decrementScore();
quiz.decrementTimer();
console.log(`Curr score: ${quiz.score}`);
}
// clears main el
mainEl.html('');
quiz.incrementCurrentQuestion();
// checks if there are any more questions.
if (quiz.currentQuestion + 1 > quiz.questions.length) {
quiz.timeLeft = 0;
displayFinalMsgForm();
} else {
displayQuestion(quiz);
}
}
const displayAnswerResponse = function(answer) {
answerResponseEl.append(`<hr><p><i>${answer}</i></p>`);
setTimeout(function(){
answerResponseEl.html('<p></p>');
}, 1000)
}
viewHighScoresBtn.on('click', displayHighScores);
mainEl.on('click', '.choice-btn', handleChoiceClicks);
mainEl.on('submit', '#initials-form', handleInitialsForm);
mainEl.on('click', '#go-back', handleGoBack);
mainEl.on('click', '#clear-highscores', handleClearHighScores);