-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
195 lines (164 loc) · 5.22 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
//data
var clickCount = 0;
var totalTime;
var questionsList = [
q1 = {
question: "Commonly used data types DO NOT include:",
choices: ["strings", "booleans", "alerts", "numbers"],
answer: "alerts"
},
q2 = {
question: "The condition in an if / else statement is enclosed within _____.",
choices: ["quotes", "curly brackets", "parentheses", "square brackets"],
answer: "parentheses"
},
q3 = {
question: "Arrays in JavaScript can be used to store _____.",
choices: ["numbers and strings", "other arrays", "booleans", "all of the above"],
answer: "all of the above"
},
q4 = {
question: "String values must be enclosed within _____ when being assigned to variables.",
choices: ["commas", "curly brackets", "quotes", "parentheses"],
answer: "quotes"
},
q5 = {
question: "A very useful too used during development and debugging for printing content to the debugger is:",
choices: ["JavaScript", "terminal / bash", "for loops", "console.log"],
answer: "console.log"
}
];
//menu
var menu = document.querySelector("#menu")
var time = document.querySelector(".time");
//homepage
var homePage = document.querySelector("#homePage");
var startQuiz = document.querySelector("#startQuiz");
//quiz
var quiz = document.querySelector("#quiz");
var questions = document.querySelector("#questions");
var result = document.querySelector("#result");
//final score
var finalScore = document.querySelector("#finalScore");
var score = document.querySelector("#score");
var inputName = document.querySelector("#inputName");
//high score
var highScores = document.querySelector("#highScores");
var scoreList = document.querySelector("#scoreList");
var goHome = document.querySelector("#goHome");
var clearScores = document.querySelector("#clearScores");
//display
homePage.style.display = "block";
quiz.style.display = "none";
finalScore.style.display = "none";
highScores.style.display = "none";
addQuestion(q1);
if (quiz.style.display === "none") {
menu.addEventListener("click", function () {
toggleDisplay(highScores);
homePage.style.display = "none";
finalScore.style.display = "none";
});
}
startQuiz.addEventListener("click", function() {
// start timer
toggleDisplay(homePage);
quiz.style.display = "block";
timer();
});
questions.addEventListener("click", function(event) {
clickCount++;
if (clickCount === 1) {
nextQuestion(event, q2, 1)
} else if (clickCount === 2) {
nextQuestion(event, q3, clickCount)
} else if (clickCount === 3) {
nextQuestion(event, q4, clickCount)
} else if (clickCount === 4) {
nextQuestion(event, q5, clickCount)
} else if (clickCount === 5) {
// all done
displayResults(event, clickCount);
menu.style.display = "flex";
}
});
finalScore.addEventListener("click", function(event) {
var userScore = parseInt(score.textContent) * 10 + parseInt(time.textContent);
event.preventDefault();
if (event.target.matches("button")) {
if (inputName.value === "") {
return;
}
var newLi = document.createElement("li");
newLi.textContent = `${inputName.value}: ${userScore}`;
scoreList.appendChild(newLi);
toggleDisplay(finalScore);
toggleDisplay(highScores);
}
})
goHome.addEventListener("click", function () {
totalTime = 60;
time.textContent = totalTime;
score.textContent = 0;
toggleDisplay(highScores);
toggleDisplay(homePage);
})
clearScores.addEventListener("click", function() {
scoreList.textContent = "";
})
function timer() {
score.textContent = 0;
clickCount = 0;
totalTime = 60;
if (totalTime >= 0) {
var timerInterval = setInterval(function() {
totalTime--;
time.textContent = totalTime;
console.log("clicks: " + clickCount)
if (clickCount === 5 || totalTime === 0) {
toggleDisplay(quiz);
toggleDisplay(finalScore);
clearInterval(timerInterval);
}
}, 1000);
}
}
function toggleDisplay(page) {
if (page.style.display === "none") {
page.style.display = "block";
} else {
page.style.display = "none";
}
}
function nextQuestion(event, name, index) {
displayResults(event, index);
clearQuestion();
addQuestion(name);
}
function displayResults(event, index) {
if (event.target.textContent === questionsList[index - 1].answer) {
result.textContent = "Correct!"
score.textContent++;
} else {
result.textContent = "Wrong!"
totalTime -= 10;
}
}
function clearQuestion() {
timeOut = setTimeout(function() {
result.textContent = "";
}, 1000)
questions.textContent = "";
}
function addQuestion(questionName) {
// h1
quiz.children[0].textContent = questionName.question;
// buttons
questionName.choices.forEach(element => {
var newButton = document.createElement("button");
newButton.textContent = element;
var br = document.createElement("br");
questions.appendChild(newButton);
questions.appendChild(br);
})
}