-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
208 lines (188 loc) Β· 5.93 KB
/
Copy pathscript.js
File metadata and controls
208 lines (188 loc) Β· 5.93 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
/// Quiz Questions ///
var questions = [
{
title: "Which primitive type can have only the values true or false? π‘",
choices: ["Number", "String", "Boolean", "Undefined"],
answer: "Boolean"
},
{
title: "The first Index of an Array is ___ π‘",
choices: ["1", "0", "Customized", "3"],
answer: "0"
},
{
title: "Which of these is not a loop type in Java? π‘",
choices: ["For", "While", "Foreach", "Loop"],
answer: "Loop"
},
{
title: "What stops the exeecution of a function? π‘",
choices: ["Go", "Stop", "Function", "Return"],
answer: "Return"
},
{
title: "Which operator takes numerical values their operands and returns a single numerical value? π‘",
choices: ["Assignment", "Arithmetic", "Logical", "Comparison"],
answer: "Arithmetic"
},
];
document.addEventListener('DOMContentLoaded', (event) => {
// Timer Variables
const initialTime = 60;
var time = 60;
var score = 0;
var qCount = 0;
var timeset;
var answers = document.querySelectorAll('#quizSection button');
// FUNCTION to set the question data in questionHolder section
var setQuestionData = () => {
queryElement('#quizSection p').innerHTML = questions[qCount].title;
queryElement('#quizSection button:nth-of-type(1)').innerHTML = `1. ${questions[qCount].choices[0]}`;
queryElement('#quizSection button:nth-of-type(2)').innerHTML = `2. ${questions[qCount].choices[1]}`;
queryElement('#quizSection button:nth-of-type(3)').innerHTML = `3. ${questions[qCount].choices[2]}`;
queryElement('#quizSection button:nth-of-type(4)').innerHTML = `4. ${questions[qCount].choices[3]}`;
}
// Use of queryElement to prevent dry code
var queryElement = (element) => {
return document.querySelector(element);
}
var onlyDisplaySection = (element) => {
var sections = document.querySelectorAll("section");
Array.from(sections).forEach((userItem) => {
userItem.classList.add('hide');
});
queryElement(element).classList.remove('hide');
}
// Reset HTML for scores
var recordsHtmlReset = () => {
queryElement('#highScores div').innerHTML = "";
var i = 1;
recordsArray.sort((a, b) => b.score - a.score);
Array.from(recordsArray).forEach(check =>
{
var scores = document.createElement("div");
scores.innerHTML = i + ". " + check.initialRecord + " - " + check.score;
queryElement('#highScores div').appendChild(scores);
i = i + 1
});
i = 0;
Array.from(answers).forEach(answer => {
answer.classList.remove('disable');
});
}
//Changes the question
var quizUpdate = (answerCopy) => {
queryElement('#scoreIndicator p').innerHTML = answerCopy;
queryElement('#scoreIndicator').classList.remove('invisible', scoreIndicator());
Array.from(answers).forEach(answer =>
{
answer.classList.add('disable');
});
// Exit quiz once questions have been answered
setTimeout(() => {
if (qCount === questions.length) {
onlyDisplaySection("#finish");
time = 0;
queryElement('#time').innerHTML = time;
} else {
setQuestionData();
Array.from(answers).forEach(answer => {
answer.classList.remove('disable');
});
}
}, 1000);
}
// Timer function
var myTimer = () => {
if (time > 0) {
time = time - 1;
queryElement('#time').innerHTML = time;
} else {
clearInterval(clock);
queryElement('#score').innerHTML = score;
onlyDisplaySection("#finish");
}
}
// Begins quiz and timer
var clock;
queryElement("#intro button").addEventListener("click", (e) => {
setQuestionData();
onlyDisplaySection("#quizSection");
clock = setInterval(myTimer, 1000);
});
// Clears timer
var scoreIndicator = () => {
clearTimeout(timeset);
timeset = setTimeout(() => {
queryElement('#scoreIndicator').classList.add('invisible');
}, 1000);
}
// Answer Controls
Array.from(answers).forEach(check => {
check.addEventListener('click', function (event) {
// If question is answered correctly!
if (this.innerHTML.substring(3, this.length) === questions[qCount].answer) {
score = score + 5;
qCount = qCount + 1;
quizUpdate("Great Job π");
}else{
// If question is answered incorrectly!
time = time - 10;
qCount = qCount + 1;
quizUpdate("Incorrect β");
}
});
});
// Error message for initials if criteria not met
var errorIndicator = () => {
clearTimeout(timeset);
timeset = setTimeout(() => {
queryElement('#errorAlert').classList.add('invisible');
}, 3000);
}
// Error handling for submitting high scores
queryElement("#records button").addEventListener("click", () => {
var initialsRecord = queryElement('#initials').value;
if (initialsRecord === ''){
queryElement('#errorAlert p').innerHTML = "You need at least 1 character";
queryElement('#errorAlert').classList.remove('invisible', errorIndicator());
} else {
recordsArray.push({
"initialRecord": initialsRecord,
"score": score
});
//Sends value to local storage for later use.
localStorage.setItem('recordsArray', JSON.stringify(recordsArray));
queryElement('#highScores div').innerHTML = '';
onlyDisplaySection("#highScores");
recordsHtmlReset();
queryElement("#initials").value = '';
}
});
// Clears High scores
queryElement("#clearScores").addEventListener("click", () => {
recordsArray = [];
queryElement('#highScores div').innerHTML = "";
localStorage.removeItem('recordsArray');
});
// Reset quiz
queryElement("#reset").addEventListener("click", () => {
time = initialTime;
score = 0;
qCount = 0;
onlyDisplaySection("#intro");
});
// Takes to high scores
queryElement("#scores").addEventListener("click", (e) => {
e.preventDefault();
clearInterval(clock);
queryElement('#time').innerHTML = 0;
time = initialTime;
score = 0;
qCount = 0;
onlyDisplaySection("#highScores");
recordsHtmlReset();
});
var recordsArray = [];
(localStorage.getItem('recordsArray')) ? recordsArray = JSON.parse(localStorage.getItem('recordsArray')): recordsArray = [];
});