-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
212 lines (199 loc) · 7.08 KB
/
script.js
File metadata and controls
212 lines (199 loc) · 7.08 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
209
210
211
212
// console.log("hello world");
console.log(window);
// DOM variables defined
var timerDisplay = document.getElementById("timer");
var mainPage = document.getElementById("main-quiz-start");
var startButton = document.getElementById("start-quiz");
var headingQuestion = document.getElementById("heading");
var answerChoices = document.getElementById("answers");
var feedbackContainer = document.getElementById("answer-feedback");
var scorePage = document.getElementById("score-page");
var submitScoreButton = document.getElementById("submit-scores");
var inputScore = document.getElementById("input-form");
var timeLeft = 75;
var interval;
var questionIndex = 0;
// Variable that will have user feedback on whether the answer was correct or wrong
var answerFeedback;
// Variable to hold the user's score during the quiz
var finalScore;
// Array of objects with all of the questions, answer choices, and the correct answer for each question
var questionBank = [
{
question: "Commonly used data types DO NOT include:",
choices: ["1. strings", "2. booleans", "3. alerts", "4. numbers"],
answer: "3. alerts",
},
{
question: "The condition in an if/else statement is enclosed within _____.",
choices: [
"1. quotes",
"2. curly brackets",
"3. parenthesis",
"4. square brackets",
],
answer: "3. parenthesis",
},
{
question: "Arrays in JavaScript can be used to store _____.",
choices: [
"1. numbers and strings",
"2. other arrays",
"3. booleans",
"4. all of the above",
],
answer: "4. all of the above",
},
{
question:
"String values must be enclosed within _____ when being assigned to variables.",
choices: ["1. commas", "2. curly brackets", "3. quotes", "4. parenthesis"],
answer: "3. quotes",
},
{
question:
"A very useful tool used during development and debugging for printing content to the debugger is:",
choices: [
"1. JavaScript",
"2. terminal/bash",
"3. for loops",
"4. console.log",
],
answer: "4. console.log",
},
];
var scoreArray = [];
function startQuiz() {
// sets the display of the instruction page to none, hiding it from the user
mainPage.style.display = "none";
// renders the question
renderQuestions(questionIndex);
}
function startTimer() {
// Displays the timer
timerDisplay.textContent = "Time: " + timeLeft;
interval = setInterval(function () {
timeLeft--;
timerDisplay.textContent = "Time: " + timeLeft;
finalScore = timeLeft;
if (timeLeft === 0) {
clearInterval(interval);
finalScore = 0;
renderScoreScreen();
}
}, 1000);
}
// Displays the questions and answers for the quiz
function renderQuestions(index) {
if (index === questionBank.length) {
clearInterval(interval);
renderScoreScreen();
} else {
answerChoices.innerHTML = "";
headingQuestion.textContent = questionBank[index].question;
// loops through all of the answer options in the choices array
for (var i = 0; i < questionBank[index].choices.length; i++) {
// Creates the answer button element
var answerButtons = document.createElement("button");
// Sets the text of the button to the first answer choice in the array
answerButtons.textContent = questionBank[index].choices[i];
answerButtons.setAttribute("class", "btn btn-primary rounded");
answerButtons.setAttribute("data-index", i);
answerChoices.appendChild(answerButtons);
}
}
}
function checkAnswer(event) {
// Checks to see if the answer button clicked is equal to the correct answer for the question
var answerIndex = event.target.getAttribute("data-index");
event.preventDefault();
if (
event.target.matches("button") &&
questionBank[questionIndex].choices[answerIndex] ===
questionBank[questionIndex].answer
) {
// If the answer is correct, the question index is incremented and the next question is displayed
answerFeedback = "Correct!";
questionIndex++;
renderQuestions(questionIndex);
} else {
// If the answer is incorrect, 10 seconds are subtracted from the time, the question index is incremented and the next question is displayed
answerFeedback = "Wrong!";
timeLeft -= 10;
questionIndex++;
renderQuestions(questionIndex);
}
}
// displays the score screen content after the game ends (either the user's time is up or they answer all questions).
function renderScoreScreen() {
var quizContainer = document.getElementById("quiz-container");
quizContainer.innerHTML = "";
// shows the score-page content
scorePage.style.visibility = "visible";
inputScore.style.visibility = "visible";
// gets the id of the paragraph element and outputs the user's score
var scoreMessage = document.getElementById("score");
scoreMessage.textContent = "Your final score is: " + finalScore;
}
// function to display either correct or wrong feedback to the user based on their answer choice
function displayFeedback(container) {
var feedBack = document.createElement("div");
feedBack.setAttribute("class", "feedback mt-3 pt-3");
feedBack.setAttribute("id", "answer-feedback");
feedBack.textContent = answerFeedback;
container.appendChild(feedBack);
}
// Function to check whether the answer was correct or wrong
function answerStatus() {
if (answerFeedback === "Correct!") {
if (questionIndex === questionBank.length) {
displayFeedback(feedbackContainer);
}
// displays the "Correct!" feedback message to the answerChoices container if the question is correct
displayFeedback(answerChoices);
feedbackTimer();
console.log("Correct!");
} else {
if (questionIndex === questionBank.length) {
displayFeedback(feedbackContainer);
}
// displays the "Wrong!" feedback message to the answerChoices container if the question is correct
displayFeedback(answerChoices);
console.log("Wrong!");
console.log("Wrong!");
feedbackTimer();
}
}
// Timer removes the feedback information of correct or wrong after 1 second
function feedbackTimer() {
var time = 1;
var interval = setInterval(function () {
time--;
if (time === 0) {
console.log("times up");
var feedbackContent = document.getElementById("answer-feedback");
feedbackContent.parentNode.removeChild(feedbackContent);
clearInterval(interval);
}
}, 800);
}
// saves the user scores to an object in localStorage
function saveScores() {
var initials = document.getElementById("initials").value;
var storedScoreArray = JSON.parse(localStorage.getItem("userScores"));
if (storedScoreArray !== null) {
scoreArray = storedScoreArray;
}
scoreArray.push({ name: initials, score: finalScore });
localStorage.setItem("userScores", JSON.stringify(scoreArray));
console.log(scoreArray);
}
startButton.addEventListener("click", startQuiz);
startButton.addEventListener("click", startTimer);
answerChoices.addEventListener("click", checkAnswer);
answerChoices.addEventListener("click", answerStatus);
submitScoreButton.addEventListener("click", function (event) {
event.preventDefault();
saveScores();
window.location.href = "highscores.html";
});