-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
431 lines (387 loc) · 15.1 KB
/
Copy pathindex.js
File metadata and controls
431 lines (387 loc) · 15.1 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// On start quiz button
// create a function to remove element by class name
function removeElementsByClass(className) {
var elements = document.getElementsByClassName(className);
while (elements.length > 0) {
elements[0].parentNode.removeChild(elements[0]);
}
}
var scoreArray = JSON.parse(localStorage.getItem("highscores")) || []; //here you need an array to push all new scores, if the local storage doesn't exist, then just default to an empty array
// change the start quiz text to stop quiz
// take the object and set it to a variable
const startButton = document.querySelector("a");
console.log(startButton);
// set variable of clicks equal to 0
let click = 0;
var value = 0;
// add an event listener to that button
startButton.addEventListener("click", initialQuestion);
var questionCounter = 0;
var timer;
var questions = [
{
question: "Commonly used data types do not include",
choices: ["booleans", "strings", "numbers", "alerts"],
answer: "alerts",
},
{
question: "Arrays in Javascript can be used to store",
choices: ["numbers", "booleans", "other arrays", "all of the above"],
answer: "all of the above",
},
{
question: "What is the correct combo definition?",
choices: [
" a standalone entity, with properties and type is an object.",
"an array is an value.",
"a string is an boolean.",
"a boolean is an array",
],
answer: "a",
},
];
var question = document.querySelector(".question");
function initialQuestion() {
timer = setInterval(showTime, 1000);
document.querySelector(".startBtn").remove();
var question = document.querySelector(".question");
question.textContent = questions[questionCounter].question;
var firstBtn = document.createElement("buttom");
firstBtn.setAttribute("id", "btn1");
firstBtn.setAttribute("class", "btn btn-primary qbtn");
firstBtn.innerHTML = questions[questionCounter].choices[0];
document.getElementById("myDiv").appendChild(firstBtn);
var secondBtn = document.createElement("buttom");
secondBtn.setAttribute("id", "btn2");
secondBtn.setAttribute("class", "btn btn-primary qbtn");
secondBtn.innerHTML = questions[questionCounter].choices[1];
document.getElementById("myDiv").appendChild(secondBtn);
var thirdBtn = document.createElement("buttom");
thirdBtn.setAttribute("id", "btn3");
thirdBtn.setAttribute("class", "btn btn-primary qbtn");
thirdBtn.innerHTML = questions[questionCounter].choices[2];
document.getElementById("myDiv").appendChild(thirdBtn);
var fourthBtn = document.createElement("buttom");
fourthBtn.setAttribute("id", "btn4");
fourthBtn.setAttribute("class", "btn btn-primary qbtn");
fourthBtn.innerHTML = questions[questionCounter].choices[3];
document.getElementById("myDiv").appendChild(fourthBtn);
firstBtn.addEventListener("click", nextQuestion);
secondBtn.addEventListener("click", nextQuestion);
thirdBtn.addEventListener("click", nextQuestion);
fourthBtn.addEventListener("click", function () {
value = value + 3.3;
nextQuestion();
});
document.querySelector("a").addEventListener("click", function () {
removeElementsByClass("qbtn");
var question = document.querySelector(".question");
clearInterval(timer);
array = [];
for (i = 0; i < scoreArray.length; i++) {
array.push(scoreArray[i].score);
}
let max = Math.max(...array);
var str1 = `
Current Score:
${String(
Math.floor(value * 10)
)}\n_\n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ `;
var str2 = `
High score:
${Math.floor(max * 10)}\n_\n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ \n_ `;
question.textContent = str1 + str2;
});
}
function nextQuestion() {
if (value * 10 == 0) {
number = number - 5;
}
questionCounter++;
console.log(questionCounter);
var question = document.querySelector(".question");
question.textContent = "What is an object in javascript";
console.log("getting clicked");
var firstBtn = document.getElementById("btn1");
var secondBtn = document.getElementById("btn2");
var thirdBtn = document.getElementById("btn3");
var fourthBtn = document.getElementById("btn4");
removeElementsByClass("qbtn");
var firstBtn1 = document.createElement("buttom");
firstBtn1.setAttribute("id", "btn1");
firstBtn1.setAttribute("class", "btn btn-primary qbtn");
firstBtn1.innerHTML = questions[questionCounter].choices[0];
document.getElementById("myDiv").appendChild(firstBtn1);
var secondBtn1 = document.createElement("buttom");
secondBtn1.setAttribute("id", "btn2");
secondBtn1.setAttribute("class", "btn btn-primary qbtn");
secondBtn1.innerHTML = questions[questionCounter].choices[1];
document.getElementById("myDiv").appendChild(secondBtn1);
var thirdBtn1 = document.createElement("buttom");
thirdBtn1.setAttribute("id", "btn3");
thirdBtn1.setAttribute("class", "btn btn-primary qbtn");
thirdBtn1.innerHTML = questions[questionCounter].choices[2];
document.getElementById("myDiv").appendChild(thirdBtn1);
var fourthBtn1 = document.createElement("buttom");
fourthBtn1.setAttribute("id", "btn4");
fourthBtn1.setAttribute("class", "btn btn-primary qbtn");
fourthBtn1.innerHTML = questions[questionCounter].choices[3];
document.getElementById("myDiv").appendChild(fourthBtn1);
firstBtn1.addEventListener("click", function () {
if (value > 0) {
nextQuestion2();
} else {
removeElementsByClass("qbtn");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
}
});
secondBtn1.addEventListener("click", function () {
if (value > 0) {
nextQuestion2();
} else {
removeElementsByClass("qbtn");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
}
});
thirdBtn1.addEventListener("click", function () {
if (value > 0) {
nextQuestion2();
} else {
removeElementsByClass("qbtn");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
}
});
fourthBtn1.addEventListener("click", function () {
value = value + 3.3;
if (value > 0) {
nextQuestion2();
} else {
removeElementsByClass("qbtn");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
}
});
}
function nextQuestion2() {
if (value * 10 == 3.3) {
number = number - 5;
}
questionCounter++;
var question = document.querySelector(".question");
question.textContent = questions[questionCounter].question;
var firstBtn3 = document.getElementById("btn111");
var secondBtn3 = document.getElementById("btn222");
var thirdBtn3 = document.getElementById("btn333");
var fourthBtn3 = document.getElementById("btn444");
removeElementsByClass("qbtn");
var firstBtn3 = document.createElement("buttom");
firstBtn3.setAttribute("id", "btn111");
firstBtn3.setAttribute("class", "btn btn-primary qbtn");
firstBtn3.innerHTML = questions[questionCounter].choices[0];
document.getElementById("myDiv").appendChild(firstBtn3);
var secondBtn3 = document.createElement("buttom");
secondBtn3.setAttribute("id", "btn222");
secondBtn3.setAttribute("class", "btn btn-primary qbtn");
secondBtn3.innerHTML = questions[questionCounter].choices[1];
document.getElementById("myDiv").appendChild(secondBtn3);
var thirdBtn3 = document.createElement("buttom");
thirdBtn3.setAttribute("id", "btn333");
thirdBtn3.setAttribute("class", "btn btn-primary qbtn");
thirdBtn3.innerHTML = questions[questionCounter].choices[2];
document.getElementById("myDiv").appendChild(thirdBtn3);
var fourthBtn3 = document.createElement("buttom");
fourthBtn3.setAttribute("id", "btn444");
fourthBtn3.setAttribute("class", "btn btn-primary qbtn");
fourthBtn3.innerHTML = questions[questionCounter].choices[3];
document.getElementById("myDiv").appendChild(fourthBtn3);
firstBtn3.addEventListener("click", function () {
value = value + 3.3;
console.log(Math.floor(value));
if (Math.floor(value) > 3.3 && Math.floor(value) < 6.6) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Kinda Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) < 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) == 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You did great , score: " + String(Math.floor(value * 10));
lastPage();
} else {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
lastPage();
}
});
secondBtn3.addEventListener("click", function () {
if (Math.floor(value) > 3.3 && Math.floor(value) < 6.6) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Kinda Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) < 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) == 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You did great , score: " + String(Math.floor(value * 10));
lastPage();
} else {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
lastPage();
}
});
thirdBtn3.addEventListener("click", function () {
if (Math.floor(value) > 3.3 && Math.floor(value) < 6.6) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Kinda Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) < 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) == 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You did great , score: " + String(Math.floor(value * 10));
lastPage();
} else {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
lastPage();
}
});
fourthBtn3.addEventListener("click", function () {
if (Math.floor(value) > 3.3 && Math.floor(value) < 6.6) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Kinda Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) < 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) == 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You did great , score: " + String(Math.floor(value * 10));
lastPage();
} else {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
lastPage();
}
});
}
function lastPage() {
clearInterval(timer);
// creating an input element
var division1 = document.createElement("div");
var input = document.createElement("input");
division1.setAttribute("class", "input-group mb-3");
input.setAttribute("class", "form-control");
var division2 = document.createElement("div");
division2.setAttribute("class", "input-group-append");
var buttonInput = document.createElement("button");
buttonInput.setAttribute("class", "btn btn-primary btn-group-justified");
buttonInput.innerHTML = " Save Score ";
buttonInput.setAttribute("class", "btn btn-outline-secondary");
document.getElementById("myDiv").appendChild(division1);
document.getElementById("myDiv").appendChild(input);
document.getElementById("myDiv").appendChild(division2);
document.getElementById("myDiv").appendChild(buttonInput);
document.getElementById("myDiv").appendChild(division1);
//this will change depending on the correct answers
var scoreArray = JSON.parse(localStorage.getItem("highscores")) || []; //here you need an array to push all new scores, if the local storage doesn't exist, then just default to an empty array
var saveScore = document.getElementById("saveScore"); //targets a button with saveScore id
buttonInput.addEventListener("click", function () {
var initials = document.getElementsByClassName("form-control"); //you will create an input element and give it this id of inputEl
console.log(initials.value);
var data = {
initials: initials.value,
score: value,
};
scoreArray.push(data);
localStorage.setItem("highscores", JSON.stringify(scoreArray));
var name = document.querySelector(".form-control").value;
document.querySelector(".form-control").remove();
document.querySelector(".btn-outline-secondary").remove();
document.querySelector(".time").remove();
document.querySelector(".question").innerHTML =
"Thank you for saving " +
name +
" your score is " +
Math.floor(scoreArray[scoreArray.length - 1].score * 10);
console.log(scoreArray);
});
}
// set time == to 60
var number = 60;
document.querySelector(".time").innerHTML = "time: " + number;
function showTime() {
if (number <= 0) {
if (Math.floor(value) > 3.3 && Math.floor(value) < 6.6) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Kinda Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) < 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You Passed, score: " + String(Math.floor(value * 10));
lastPage();
} else if (Math.floor(value) > 6.6 && Math.floor(value) == 9) {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You did great , score: " + String(Math.floor(value * 10));
lastPage();
} else {
removeElementsByClass("qbtn");
removeElementsByClass("title");
question.textContent =
"You failed, score: " + String(Math.floor(value * 10));
lastPage();
}
} else {
number--;
document.querySelector(".time").innerHTML = "time: " + number;
}
}