-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_quiz.js
320 lines (220 loc) · 11.3 KB
/
code_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
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
$(document).ready(function() {
//these are arrays of the questions and answers
var answerA = ["A javaScript library.", "Connecting to a server.", "Terminal", "Copy", "<>", "Disregard Order Message", "%", "DOM", "The content looks good in most viewports, regardless of size.", "*"];
var answerB = ["A user manual.", "Factory restart.", "Method", "Scroll", "::", "Days Over Minutes", "#", "Dividend", "The application functions without internet connection.", "$"];
var answerC = ["A database.", "Styling HTML.", "Single", "Hover", "{}", "Document Object Model", "thisID*", "Element", "The user is expected to respond to a series of prompts.", "@"];
var answerD = ["A question in Java.", "Mathematical applications.", "Round", "Click", "[]", "Data Ordering Model", "$", "Compartment", "Everything that is written is converted to audio wave files.", "&"];
var question = ["What is jQuery?", "What is CSS used for?", "What is the term for a function within an object?", "Which of the following is not an example of an event.", "In javaScript, which kind of enclosure is used in an array?", "What do the letters in DOM stand for?", "In CSS, which of the following denotes an ID?", "A <div> is an example of an HTML ___________ .", "What is a common feature of a responsive layout?", "Which symbol is most commonly found as the intial character in code related to jQuery?"];
//these are the correct responses that will later be compared to the users' resposnes
var correctAnswer = [1, 3, 2, 1, 4, 3, 2, 3, 1, 2];
//each correct response will push an item into the keepScore array. At the end, the length of this array will be divided by the number of questions (10) and multiplied by 100 to return the user's score.
var keepScore = [];
//these arrays will hold the user's names and scores which will then be stored in Local Storage.
var userHighScores = [];
var storedUserNames = [];
//these are the divs that let the user know if the previous answer was correct or incorrect. Depending on the response the appropriate div will be shown.
$('#correct').prepend('<img id="hey" src="assets/images/hey.png" />').prepend('<img id="checkmark" src="assets/images/checkmark.png" />');
$('#incorrect').prepend('<img id="ohno" src="assets/images/ohno.png" />').prepend('<img id="xmark" src="assets/images/xmark.png" />');
// these are all the divs that will be hidden until they need to be shown.
$("#correct").hide();
$("#incorrect").hide();
$("#questions-prompt").hide();
$("#high-scores-prompt").hide();
$("#save-score-prompt").hide();
//this function will display the questions and answers in the questions prompt div. It grabs the info from the arrays declared up top.
var i;
function nextQuestion() {
$("#question").text(question[i]);
$("#checkbox-a").text(answerA[i]);
$("#checkbox-b").text(answerB[i]);
$("#checkbox-c").text(answerC[i]);
$("#checkbox-d").text(answerD[i]);
}
//upon clicking "start quiz" the timer starts to count down and the first question and answers are displayed
$("#start-quiz").on("click", function setTime() {
$("#starting-prompt").hide();
$("#questions-prompt").show();
i = 0;
nextQuestion();
var timeLeft = 30;
var timeInterval = setInterval(function() {
$("#time-left").text(timeLeft + " Seconds");
timeLeft--;
//if the clock runs out, stop the clock and display 0 seconds on the timer
if (timeLeft <= 0) {
clearInterval(timeInterval);
$("#time-left").text(0 + " Seconds");
//hide the questions prompt, calculate the score, show score
$("#questions-prompt").hide();
var yourScore = keepScore.length / 10 * 100;
$("#your-score").append("Your score is " + yourScore + "%");
$("#save-score-prompt").show();
//save user name
$("#user-submit-button").on("click", function(event) {
event.preventDefault();
$("#save-score-prompt").hide();
userName = $('#user-name').val();
//get previously saved entries off local storage
var theseNames = JSON.parse(localStorage.getItem("storedNames"));
//if there's nothing in there, stop the function. Otherwise, push the contents into storedUserNames array
if (theseNames !== null) {
for (var i = 0; i < theseNames.length; i++) {
storedUserNames.push(theseNames[i]);
}
}
//now push the newly submitted user name into the array
storedUserNames.push(userName);
//now we store the updated storedUserNames array into Local Storage
localStorage.setItem('storedNames', JSON.stringify(storedUserNames));
//the following code, does the same for the user scores as was done for user names
var theseScores = JSON.parse(localStorage.getItem("storedScores"));
if (theseScores !== null) {
for (var i = 0; i < theseScores.length; i++) {
userHighScores.push(theseScores[i]);
}
}
userHighScores.push(yourScore);
localStorage.setItem('storedScores', JSON.stringify(userHighScores));
//now show the high-scores-prompt div and run the render function which grabs info from local storage and shows it here
$("#high-scores-prompt").show();
render();
});
}
//the following does the same thing as the previous block of code but instead of on the condition of running out of time, it follows the condition that all the questions have been answered.
else if (i === answerA.length) {
clearInterval(timeInterval);
timeLeft = 0;
$("#time-left").text(timeLeft + " Seconds");
$("#questions-prompt").hide();
var yourScore = keepScore.length / 10 * 100;
$("#your-score").append("Your score is " + yourScore + "%");
$("#save-score-prompt").show();
$("#user-submit-button").on("click", function(event) {
event.preventDefault();
$("#save-score-prompt").hide();
userName = $('#user-name').val();
score = yourScore;
var theseNames = JSON.parse(localStorage.getItem("storedNames"));
if (theseNames !== null) {
for (var i = 0; i < theseNames.length; i++) {
storedUserNames.push(theseNames[i]);
}
}
storedUserNames.push(userName);
localStorage.setItem('storedNames', JSON.stringify(storedUserNames));
var theseScores = JSON.parse(localStorage.getItem("storedScores"));
if (theseScores !== null) {
for (var i = 0; i < theseScores.length; i++) {
userHighScores.push(theseScores[i]);
}
}
userHighScores.push(yourScore);
localStorage.setItem('storedScores', JSON.stringify(userHighScores));
$("#high-scores-prompt").show();
render();
});
}
}, 1000);
//the following code takes the user's answer and puts it into the userAnswer array which compares it to the correct answer array up top.
//after comparing the answer to the correct ones, the "Correct!" or "Incorrect!" div is shown
//if the answer is correct, a value of 1 is pushed into the keepScore div which is then used to calculate the score
var userAnswer = [];
$("#checkbox-a").on("click", function logLetter() {
userAnswer.push(1);
x = userAnswer.length - 1;
if (correctAnswer[x] === userAnswer[x]) {
$("#correct").show();
setTimeout(function() { $("#correct").hide(); }, 1000);
keepScore.push(1);
}
else {
$("#incorrect").show();
setTimeout(function() { $("#incorrect").hide(); }, 1000);
timeLeft = timeLeft - 5;
}
})
$("#checkbox-b").on("click", function logLetter() {
userAnswer.push(2);
x = userAnswer.length - 1;
if (correctAnswer[x] === userAnswer[x]) {
$("#correct").show();
setTimeout(function() { $("#correct").hide(); }, 1000);
keepScore.push(1);
}
else {
$("#incorrect").show();
setTimeout(function() { $("#incorrect").hide(); }, 1000);
timeLeft = timeLeft - 5;
}
})
$("#checkbox-c").on("click", function logLetter() {
userAnswer.push(3);
x = userAnswer.length - 1;
if (correctAnswer[x] === userAnswer[x]) {
$("#correct").show();
setTimeout(function() { $("#correct").hide(); }, 1000);
keepScore.push(1);
}
else {
$("#incorrect").show();
setTimeout(function() { $("#incorrect").hide(); }, 1000);
timeLeft = timeLeft - 5;
}
})
$("#checkbox-d").on("click", function logLetter() {
userAnswer.push(4);
x = userAnswer.length - 1;
if (correctAnswer[x] === userAnswer[x]) {
$("#correct").show();
setTimeout(function() { $("#correct").hide(); }, 1000);
keepScore.push(1);
}
else {
$("#incorrect").show();
setTimeout(function() { $("#incorrect").hide(); }, 1000);
timeLeft = timeLeft - 5;
}
})
});
//this cycles through the appropriate questions and answers in the arrays declared up top.
$(".question-button").on("click", function newQuestion() {
i++;
nextQuestion();
});
$("#start-again-button").on("click", function newGame() {
location.reload();
});
});
//go back button
$("#go-back").on("click", function () {
location.reload();
})
//clear scores button
$("#clear-high-scores").on("click", function(){
$("#high-scores").text("");
localStorage.clear("storedScores");
localStorage.clear("storedNames");
})
//high scores tab
$("#high-scores-tab").on("click", function() {
$("#high-scores-prompt").show();
$("#starting-prompt").hide();
$("#questions-prompt").hide();
$("#save-score-prompt").hide();
render();
})
function previousScores() {
localStorage.getItem('storedScores', JSON.stringify(userHighScores));
}
//renders score intro the high scores prompt div, grabbing the info from local storage
function render() {
var renderScore = JSON.parse(localStorage.getItem("storedScores"));
var renderNames = JSON.parse(localStorage.getItem("storedNames"));
$("#high-scores").html("");
for (var i = 0; i < renderScore.length; i++) {
thisScore = renderScore[i];
thisName = renderNames[i];
x = i + 1;
$("#high-scores").append("<li>" + " " + x + ". " + thisName + " | " + thisScore + "</li>");
}
}