Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
196 lines (157 loc) · 6.09 KB
/
Copy pathscript.js
File metadata and controls
196 lines (157 loc) · 6.09 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
// initialize DOM variables
var startButton = document.getElementById("start");
var timerEl = document.getElementById("timer");
var startDiv = document.getElementById("start-screen");
var questionsDiv = document.getElementById("questions");
var endDiv = document.getElementById("endscreen");
var answersDiv = document.getElementById("choices");
var scoreSpan = document.getElementById("finalScore");
var submitButton = document.getElementById("submit");
var highScoreTable = document.getElementById("scoretable");
var highScoresDiv = document.getElementById("highScores");
var clearHighScores = document.getElementById("clear-high-scores");
var viewHighScoresLink = document.getElementById("highscoreslink");
var goBackButton = document.getElementById("go-back");
var storedHighScores = JSON.parse(localStorage.getItem("highScoreList")) || []; // Retrieve old high scores
// Count starts at 75 seconds
var secondsLeft = 75;
var questionIndex = 0;
var highScoreList = {
name: "",
score: 0
};
// Questions and answers stored in objects nested in an array
var questions = [
//Question 1
{
question: "Commonly used data types DO NOT include:", //0
answers: ["strings", "booleans", "numbers", "alerts"],
correctAnswer: "alerts",
},
// Question 2
{
question: "The condition in an if / else statement is enclosed within ____.", //1
answers: ["quotes", "curly brackets", "parentheses", "square brackets"],
correctAnswer: "parentheses",
},
//Question 3
{
question: "Arrays in JavaScript can be used to store ____.", //2
answers: ["numbers and strings", "other arrays", "booleans", "all of the above"],
correctAnswer: "all of the above"
},
// Question 4
{
question: "String values must be enclosed within ____ when being assigned to variables.", //3
answers: ["commas", "curly brackets", "quotes", "parentheses"],
correctAnswer: "quotes",
},
// Question 5
{
question: "A very useful tool used during development and debugging for printing content to the debugger is:", //4
answers: ["JavaScript", "terminal / bash", "for loops", "console.log"],
correctAnswer: "console.log"
}
]
function startGame() {
startDiv.setAttribute("class", "hide");
questionsDiv.removeAttribute("class");
highScoresDiv.setAttribute("class", "hide");
startTimer();
changeQuestions();
}
// Start the timer when start button is clicked
function startTimer() {
var timeInterval = setInterval(function () {
secondsLeft--;
timerEl.textContent = "Time: " + secondsLeft;
if (secondsLeft <= 0 && questionIndex !== questions.length) {
clearInterval(timeInterval);
gameOver();
}
}, 1000);
};
// Displaying a new question
function changeQuestions() {
var currentQuestion = questions[questionIndex];
var questionDisplay = document.getElementById("questions-title"); // Display question
questionDisplay.textContent = currentQuestion.question;
answersDiv.innerHTML = "";
currentQuestion.answers.forEach(function (answer) {
var answerButton = document.createElement("button");
answerButton.setAttribute("class", "choice");
answerButton.setAttribute("value", answer);
answerButton.textContent = answer;
answerButton.onclick = questionCheck;
answersDiv.appendChild(answerButton);
})
}
function questionCheck() {
// Check if the choice doesn't match the correct answer
console.log(this.value);
if (this.value !== questions[questionIndex].correctAnswer) {
secondsLeft -= 10;
alert("Incorrect!");
} else {
alert("Correct!");
}
questionIndex++;
if (questionIndex == questions.length) {
//console.log(questionIndex, this.value, questions[questionIndex].correctAnswer);
gameOver();
} else {
changeQuestions();
}
}
function gameOver() { // When they go through all the questions or the timer runs out
var finalScore = secondsLeft;
questionsDiv.setAttribute("class", "hide");
endDiv.removeAttribute("class");
timerEl.setAttribute("class", "hide");
scoreSpan.textContent = finalScore;
submitButton.addEventListener("click", function () {
userInitials = document.querySelector("#initials").value;
console.log(userInitials);
var userScore = {
initials: userInitials,
score: finalScore
};
storedHighScores.push(userScore);
storedHighScores.sort(compare);
localStorage.setItem("highScoreList", JSON.stringify(storedHighScores));
endDiv.setAttribute("class", "hide");
highScoresDiv.removeAttribute("class");
for (var i = 0; i < storedHighScores.length; i++) { // Build high score table
var row = document.createElement("tr"); // Create row
var cell = document.createElement("td"); //Create cell
cell.textContent = (i + 1) + ". " + storedHighScores[i].initials + " " + storedHighScores[i].score; // Display each initial and score
row.appendChild(cell);
highScoreTable.appendChild(row);
}
})
}
function compare(player1, player2) { // Sort the high scores from highest to lowest
return player2.score - player1.score;
}
function clearStorage() { // Clear local storage
localStorage.clear();
}
// When following "View High Scores" link in upper lefthand corner of the page
function viewHighScores() {
startDiv.setAttribute("class", "hide");
questionsDiv.setAttribute("class", "hide");
endDiv.setAttribute("class", "hide");
highScoresDiv.removeAttribute("class");
timerEl.setAttribute("class", "hide");
}
function goBack() {
location.reload();
}
// Event listener for start button
startButton.addEventListener("click", startGame);
// Event listener for clear button
clearHighScores.addEventListener("click", clearStorage);
// Event listener for View High Scores link
viewHighScoresLink.addEventListener("click", viewHighScores);
// Event listener for Go Back button
goBackButton.addEventListener("click", goBack);