-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
171 lines (133 loc) · 4.16 KB
/
logic.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
var PORT = process.env.PORT || 5000;
// variables to keep track of quiz state
var currentQuestionIndex = 0;
var time = questions.length * 15;
var timerId;
// variables to reference DOM elements
var questionsEl = document.getElementById("questions");
var timerEl = document.getElementById("time");
var choicesEl = document.getElementById("choices");
var submitBtn = document.getElementById("submit");
var startBtn = document.getElementById("start");
var initialsEl = document.getElementById("initials");
var feedbackEl = document.getElementById("feedback");
// sound effects
var sfxRight = new Audio("assets/sfx/correct.wav");
var sfxWrong = new Audio("assets/sfx/incorrect.wav");
function startQuiz() {
// hide start screen
var startScreenEl = document.getElementById("start-screen");
startScreenEl.setAttribute("class", "hide");
// un-hide questions section
questionsEl.removeAttribute("class");
// start timer
timerId = setInterval(clockTick, 1000);
// show starting time
timerEl.textContent = time;
getQuestion();
}
function getQuestion() {
// get current question object from array
var currentQuestion = questions[currentQuestionIndex];
// update title with current question
var titleEl = document.getElementById("question-title");
titleEl.textContent = currentQuestion.title;
// clear out any old question choices
choicesEl.innerHTML = "";
// loop over choices
currentQuestion.choices.forEach(function(choice, i) {
// create new button for each choice
var choiceNode = document.createElement("button");
choiceNode.setAttribute("class", "choice");
choiceNode.setAttribute("value", choice);
choiceNode.textContent = i + 1 + ". " + choice;
// attach click event listener to each choice
choiceNode.onclick = questionClick;
// display on the page
choicesEl.appendChild(choiceNode);
});
}
function questionClick() {
// check if user guessed wrong
if (this.value !== questions[currentQuestionIndex].answer) {
// penalize time
time -= 15;
if (time < 0) {
time = 0;
}
// display new time on page
timerEl.textContent = time;
// play "wrong" sound effect
sfxWrong.play();
feedbackEl.textContent = "Wrong!";
} else {
// play "right" sound effect
sfxRight.play();
feedbackEl.textContent = "Correct!";
}
// flash right/wrong feedback on page for half a second
feedbackEl.setAttribute("class", "feedback");
setTimeout(function() {
feedbackEl.setAttribute("class", "feedback hide");
}, 1000);
// move to next question
currentQuestionIndex++;
// check if we've run out of questions
if (currentQuestionIndex === questions.length) {
quizEnd();
} else {
getQuestion();
}
}
function quizEnd() {
// stop timer
clearInterval(timerId);
// show end screen
var endScreenEl = document.getElementById("end-screen");
endScreenEl.removeAttribute("class");
// show final score
var finalScoreEl = document.getElementById("final-score");
finalScoreEl.textContent = time;
// hide questions section
questionsEl.setAttribute("class", "hide");
}
function clockTick() {
// update time
time--;
timerEl.textContent = time;
// check if user ran out of time
if (time <= 0) {
quizEnd();
}
}
function saveHighscore() {
// get value of input box
var initials = initialsEl.value.trim();
// make sure value wasn't empty
if (initials !== "") {
// get saved scores from localstorage, or if not any, set to empty array
var highscores =
JSON.parse(window.localStorage.getItem("highscores")) || [];
// format new score object for current user
var newScore = {
score: time,
initials: initials
};
// save to localstorage
highscores.push(newScore);
window.localStorage.setItem("highscores", JSON.stringify(highscores));
// redirect to next page
window.location.href = "highscores.html";
}
}
function checkForEnter(event) {
// "13" represents the enter key
if (event.key === "Enter") {
saveHighscore();
}
}
// user clicks button to submit initials
submitBtn.onclick = saveHighscore;
// user clicks button to start quiz
startBtn.onclick = startQuiz;
initialsEl.onkeyup = checkForEnter;