-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
212 lines (182 loc) · 6.39 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
202
203
204
205
206
207
208
209
210
211
212
// variables set
var quizBody = document.getElementById("quiz");
var resultsEl = document.getElementById("result");
var finalScoreEl = document.getElementById("finalScore");
var gameoverDiv = document.getElementById("gameover");
var questionsEl = document.getElementById("questions");
var quizTimer = document.getElementById("timer");
var startQuizButton = document.getElementById("startbtn");
var startQuizDiv = document.getElementById("startpage");
var highscoreContainer = document.getElementById("highscoreContainer");
var highscoreDiv = document.getElementById("high-scorePage");
var highscoreInputName = document.getElementById("initials");
var highscoreDisplayName = document.getElementById("highscore-initials");
var endGameBtns = document.getElementById("endGameBtns");
var submitScoreBtn = document.getElementById("submitScore");
var highscoreDisplayScore = document.getElementById("highscore-score");
var buttonA = document.getElementById("a");
var buttonB = document.getElementById("b");
var buttonC = document.getElementById("c");
var buttonD = document.getElementById("d");
// questions about the NBA teams
var quizQuestions = [{
question: "What does NBA stand for?",
choiceA: "No boys allowed",
choiceB: "National Basketball Association",
choiceC: "New basketball association",
choiceD: "Netflix best adds",
correctAnswer: "b"
},
{
question: "Who won the first ever NBA championship?",
choiceA: "Golden state worriors",
choiceB: "Lakers",
choiceC: "Chicago bulls",
choiceD: "Boston Celtics",
correctAnswer: "d"
},
{
question: "Which NBA franchise has won the most championships?",
choiceA: "Chicago bulls",
choiceB: "Denver nuggets",
choiceC: "Boston celtics",
choiceD: "Portland Trail blazers",
correctAnswer: "c"
},
{
question: "What NBA team did Micheal Jordan play for?",
choiceA: "Sixers",
choiceB: "Miami Heat",
choiceC: "Dallas mavericks",
choiceD: "chicago Bulls",
correctAnswer: "d"
},
{
question: "What NBA team does Stephen Curry play for?",
choiceA: "Raptors",
choiceB: "Golden state warriors",
choiceC: "San Antonio Spurs",
choiceD: "Houston rockets",
correctAnswer: "b"
},
];
var finalQuestionIndex = quizQuestions.length;
var currentQuestionIndex = 0;
var timeLeft = 30;
var timerInterval;
var score = 0;
var correct;
// generate questions and answers.
function generateQuizQuestion() {
gameoverDiv.style.display = "game over";
if (currentQuestionIndex === finalQuestionIndex) {
return showScore();
}
var currentQuestion = quizQuestions[currentQuestionIndex];
questionsEl.innerHTML = "<p>" + currentQuestion.question + "</p>";
buttonA.innerHTML = currentQuestion.choiceA;
buttonB.innerHTML = currentQuestion.choiceB;
buttonC.innerHTML = currentQuestion.choiceC;
buttonD.innerHTML = currentQuestion.choiceD;
};
// first quiz question.
function startQuiz() {
gameoverDiv.style.display = "none";
startQuizDiv.style.display = "none";
generateQuizQuestion();
timerInterval = setInterval(function () {
timeLeft--;
quizTimer.textContent = "Time left: " + timeLeft;
if (timeLeft === 0) {
clearInterval(timerInterval);
showScore();
}
}, 1000);
quizBody.style.display = "block";
}
// dispaly score
function showScore() {
quizBody.style.display = "none"
gameoverDiv.style.display = "flex";
clearInterval(timerInterval);
highscoreInputName.value = "";
finalScoreEl.innerHTML = "You got " + score + " out of " + quizQuestions.length + " correct!";
}
// submit score
submitScoreBtn.addEventListener("click", function highscore() {
// initials cant be empty alert
if (highscoreInputName.value === "") {
alert("enter desired initials");
return false;
} else {
var savedHighscores = JSON.parse(localStorage.getItem("savedHighscores")) || [];
var currentUser = highscoreInputName.value.trim();
var currentHighscore = {
name: currentUser,
score: score
};
gameoverDiv.style.display = "none";
highscoreContainer.style.display = "flex";
highscoreDiv.style.display = "block";
endGameBtns.style.display = "flex";
savedHighscores.push(currentHighscore);
localStorage.setItem("savedHighscores", JSON.stringify(savedHighscores));
generateHighscores();
}
});
// This function clears the list for the high scores and generates a new high score list from local storage
function generateHighscores() {
highscoreDisplayName.innerHTML = "";
highscoreDisplayScore.innerHTML = "";
var highscores = JSON.parse(localStorage.getItem("savedHighscores")) || [];
for (i = 0; i < highscores.length; i++) {
var newNameSpan = document.createElement("li");
var newScoreSpan = document.createElement("li");
newNameSpan.textContent = highscores[i].name;
newScoreSpan.textContent = highscores[i].score;
highscoreDisplayName.appendChild(newNameSpan);
highscoreDisplayScore.appendChild(newScoreSpan);
}
}
// display high scores
function showHighscore() {
startQuizDiv.style.display = "none"
gameoverDiv.style.display = "none";
highscoreContainer.style.display = "flex";
highscoreDiv.style.display = "block";
endGameBtns.style.display = "flex";
generateHighscores();
}
function clearScore() {
window.localStorage.clear();
highscoreDisplayName.textContent = "";
highscoreDisplayScore.textContent = "";
}
// reset the quiz
function replayQuiz() {
highscoreContainer.style.display = "none";
gameoverDiv.style.display = "none";
startQuizDiv.style.display = "flex";
timeLeft = 30;
score = 0;
currentQuestionIndex = 0;
}
// verify the answer to see if its correct
function checkAnswer(answer) {
correct = quizQuestions[currentQuestionIndex].correctAnswer;
if (answer === correct && currentQuestionIndex !== finalQuestionIndex) {
score++;
alert("!Great! U GOT IT.");
currentQuestionIndex++;
generateQuizQuestion();
} else if (answer !== correct && currentQuestionIndex !== finalQuestionIndex) {
alert("Ooops, sorry try again.")
startTime -= 5;
currentQuestionIndex++;
generateQuizQuestion();
} else {
showScore();
}
}
// start quiz
startQuizButton.addEventListener("click", startQuiz);