-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
270 lines (203 loc) · 6.35 KB
/
javascript.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
var welcomDiv = document.querySelector(".welcomePage");
var questionsDiv = document.querySelector(".questionsDiv");
var startBtn = document.querySelector(".startBtn");
var questionsH1 = document.querySelector(".questions");
var appendHere = document.querySelector(".appendHere");
var answerBtn = document.querySelector(".answerBtn");
var playButton = document.querySelector("#play");
var minutesDisplay = document.querySelector("#minutes");
var secondsDisplay = document.querySelector("#seconds");
var row = document.querySelector(".row")
var resultsDiv = document.querySelector(".resultsPage")
var submitBtn = document.querySelector(".submitBtn");
var results = document.querySelector(".results");
var numbercorrect=0;
var highscores = document.querySelector(".scorePtag")
var highscoresDiv = document.querySelector(".highscores")
var initials = document.querySelector("#initials");
var totalSeconds = 0;
var secondsElapsed = 0;
var index = 0;
var interval;
var i = 0;
var g = 0;
var scoreList = [];
//start of timer
if (localStorage.getItem("scoreList")) {
scoreList = JSON.parse(localStorage.getItem("scoreList"))
}
function showScores() {
highscoresDiv.innerHTML = "";
for (var i = 0; i < scoreList.length; i++) {
console.log("HERE:", scoreList[i][0] + scoreList[i][1])
var par = document.createElement("p");
par.innerText = scoreList[i][0] + " " + scoreList[i][1]
highscoresDiv.appendChild(par)
}
}
showScores()
function getFormattedMinutes() {
//
var secondsLeft = totalSeconds - secondsElapsed;
var minutesLeft = Math.floor(secondsLeft / 60);
// var formattedMinutes;
if (minutesLeft < 10) {
formattedMinutes = "0" + minutesLeft;
} else {
formattedMinutes = minutesLeft;
}
// return formattedMinutes;
}
function getFormattedSeconds() {
var secondsLeft = (totalSeconds - secondsElapsed) % 60;
var formattedSeconds;
if (secondsLeft < 10) {
formattedSeconds = "0" + secondsLeft;
} else {
formattedSeconds = secondsLeft;
}
return formattedSeconds;
}
function setTime() {
var minutes;
minutes = 1
clearInterval(interval);
totalSeconds = minutes * 60;
}
function renderTime() {
// When renderTime is called it sets the textContent for the timer html...
minutesDisplay.textContent = getFormattedMinutes();
secondsDisplay.textContent = getFormattedSeconds();
// ..and then checks to see if the time has run out
if (secondsElapsed >= totalSeconds) {
if (totalSeconds === 0 && minutes === 0) {
}
stopTimer();
// alert("Time's Up");
questionsDiv.style.display= "none";
resultsDiv.style.display= "block";
results.textContent = numbercorrect;
submitBtn.addEventListener("click", function(e) {
console.log('something')
e.preventDefault();
var userint = initials.value
console.log("hey")
console.log(userint)
if (userint.length > 2 || initials.value.length == 0) {
alert("You need at most two characters")
}
else if(highscoresDiv.style.display=== "block"){
window.location.reload();
}
else {
var p = [userint, numbercorrect];
console.log("P:", p)
scoreList.push(p)
localStorage.setItem("scoreList", JSON.stringify(scoreList));
showScores();
}
});
//tried to have a reset test button but couldnt figure it out
/*
submitBtn.addEventListener("click", function(){
welcomDiv.style.display="none"
questionsDiv.style.display= "block"
showQuestions();
});
*/
}
}
function startTimer() {
setTime();
interval = setInterval(function() {
secondsElapsed++;
renderTime();
}, 1000);
}
function stopTimer() {
secondsElapsed = 0;
setTime();
renderTime();
}
startBtn.addEventListener("click", startTimer);
// end of timer
var questionsArray=[
{
question:"Who invented JavaScript?",
options:["Douglas Crockford","Sheryl Sandberg","Brendan Eich"],
answer:"Brendan Eich"
},
{
question:"What is an h1 tag?",
options:["<h1>","<div>", "<script>"],
answer:"<h1>"
},
{
question:"Which is a self closing tag?",
options:["<img>","<h3>", "<h1>"],
answer:"<img>"
},
{
question:"Which one of these is a JavaScript package manager?",
options:["Node.js","TypeScript", "npm"],
answer:"npm"
},
{
question:"Which tool can you use to ensure code quality?",
options:["Angular","jQuery", "RequireJS","ESLint"],
answer:"ESLint"
}
]
totalSeconds -=60;
questionsDiv.style.display= "none";
resultsDiv.style.display= "none";
highscoresDiv.style.display= "none";
highscores.addEventListener("click", function(){
welcomDiv.style.display="none";
questionsDiv.style.display= "none";
resultsDiv.style.display= "none";
highscoresDiv.style.display= "block"
});
startBtn.addEventListener("click", function(){
welcomDiv.style.display="none";
questionsDiv.style.display= "block";
showQuestions();
});
function showQuestions(){
questionsH1.textContent= questionsArray[index].question
var myDiv= document.createElement("div")
myDiv.setAttribute("class","row")
for (i=0;i<questionsArray[index].options.length; i++){
var button =document.createElement("button")
button.setAttribute("class","btn btn-primary answerBtn col-12")
button.setAttribute("type","button")
button.textContent =questionsArray[index].options[i]
myDiv.appendChild(button);
};
appendHere.appendChild(myDiv);
console.log(g)
console.log (questionsArray.length)
}
answerBtn.addEventListener("click", function(event){
if (event.target.innerText == questionsArray[index].answer){
alert("Correct");
numbercorrect++;
g++;
}
else {
alert("wrong answer")
//minus 10 seconds from clock
//totalSeconds = totalSeconds-10;
totalSeconds-=20;
g++;
};
if (g===questionsArray.length){
totalSeconds=0;
}
else {
index++;
console.log(questionsArray.length)
appendHere.innerHTML = "";
showQuestions();
}
});