-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
201 lines (173 loc) · 6.46 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
196
197
198
199
200
201
// Var with array and object for questions
var questions = [
{
title: "Commonly used data types DO NOT include:",
choices: ["strings", "booleans", "alerts", "numbers"],
answer: "alerts"
},
{
title: "The condition in an if / else statement is enclosed within ____.",
choices: ["quotes", "curly brackets", "parentheses", "square brackets"],
answer: "parentheses"
},
{
title: "Arrays in Javascript can be used to store ____.",
choices: ["numbers and strings", "other arrays", "booleans", "all of the above"],
answer: "all of the above"
},
{
title: "String values must be enclosed within ____ when being assigned to variables.",
choices: ["commas", "curly brackets", "quotes", "parenthesis"],
answer: "quotes"
},
{
title: "A very useful tool for used during development and debugging for printing content to the debugger is:",
choices: ["Javascript", "terminal / bash", "for loops", "console log"],
answer: "console log"
},
];
// Declared variables
var score = 0;
var questionIndex = 0;
// Start working code
// Declared variables
var currentTime = document.querySelector("#currentTime");
var timer = document.querySelector("#startTime");
var questionsDiv = document.querySelector("#questionsDiv");
var wrapper = document.querySelector("#wrapper");
// Seconds left is 15 seconds per question:
var secondsLeft = 76;
// Holds interval time
var holdInterval = 0;
// Holds penalty time
var penalty = 10;
// Creates new element
var ulCreate = document.createElement("ul");
// Triggers timer on button, shows user a display on the screen
timer.addEventListener("click", function () {
// We are checking zero because its originally set to zero
if (holdInterval === 0) {
holdInterval = setInterval(function () {
secondsLeft--;
currentTime.textContent = "Time: " + secondsLeft;
if (secondsLeft <= 0) {
clearInterval(holdInterval);
allDone();
currentTime.textContent = "Time's up!";
}
}, 1000);
}
render(questionIndex);
});
// Renders questions and choices to page:
function render(questionIndex) {
// Clears existing data
questionsDiv.innerHTML = "";
ulCreate.innerHTML = "";
// For loops to loop through all info in array
for (var i = 0; i < questions.length; i++) {
// Appends question title only
var userQuestion = questions[questionIndex].title;
var userChoices = questions[questionIndex].choices;
questionsDiv.textContent = userQuestion;
}
// New for each for question choices
userChoices.forEach(function (newItem) {
var listItem = document.createElement("li");
listItem.textContent = newItem;
questionsDiv.appendChild(ulCreate);
ulCreate.appendChild(listItem);
listItem.addEventListener("click", (compare));
})
}
// Event to compare choices with answer
function compare(event) {
var element = event.target;
if (element.matches("li")) {
var createDiv = document.createElement("div");
createDiv.setAttribute("id", "createDiv");
// Correct condition
if (element.textContent == questions[questionIndex].answer) {
score++;
createDiv.textContent = "Correct! The answer is: " + questions[questionIndex].answer;
// Correct condition
} else {
// Will deduct -5 seconds off secondsLeft for wrong answers
secondsLeft = secondsLeft - penalty;
createDiv.textContent = "Wrong! The correct answer is: " + questions[questionIndex].answer;
}
}
// Question Index determines number question user is on
questionIndex++;
if (questionIndex >= questions.length) {
// All done will append last page with user stats
allDone();
createDiv.textContent = "End of quiz!" + " " + "You got " + score + "/" + questions.length + " Correct!";
} else {
render(questionIndex);
}
questionsDiv.appendChild(createDiv);
}
// All done will append last page
function allDone() {
questionsDiv.innerHTML = "";
currentTime.innerHTML = "";
// Heading:
var createH1 = document.createElement("h1");
createH1.setAttribute("id", "createH1");
createH1.textContent = "All Done!"
questionsDiv.appendChild(createH1);
// Paragraph
var createP = document.createElement("p");
createP.setAttribute("id", "createP");
questionsDiv.appendChild(createP);
// Calculates time remaining and replaces it with score
if (secondsLeft >= 0) {
var timeRemaining = secondsLeft;
var createP2 = document.createElement("p");
clearInterval(holdInterval);
createP.textContent = "Your final score is: " + timeRemaining;
questionsDiv.appendChild(createP2);
}
// Label
var createLabel = document.createElement("label");
createLabel.setAttribute("id", "createLabel");
createLabel.textContent = "Enter your initials: ";
questionsDiv.appendChild(createLabel);
// input
var createInput = document.createElement("input");
createInput.setAttribute("type", "text");
createInput.setAttribute("id", "initials");
createInput.textContent = "";
questionsDiv.appendChild(createInput);
// submit
var createSubmit = document.createElement("button");
createSubmit.setAttribute("type", "submit");
createSubmit.setAttribute("id", "Submit");
createSubmit.textContent = "Submit";
questionsDiv.appendChild(createSubmit);
// Event listener to capture initials and local storage for initials and score
createSubmit.addEventListener("click", function () {
var initials = createInput.value;
if (initials === null) {
console.log("No value entered!");
} else {
var finalScore = {
initials: initials,
score: timeRemaining
}
console.log(finalScore);
var allScores = localStorage.getItem("allScores");
if (allScores === null) {
allScores = [];
} else {
allScores = JSON.parse(allScores);
}
allScores.push(finalScore);
var newScore = JSON.stringify(allScores);
localStorage.setItem("allScores", newScore);
// Travels to final page
window.location.replace("./HighScores.html");
}
});
}