-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
165 lines (143 loc) · 5.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
homePage = document.getElementById("homePage-container");
quizStart = document.getElementById("questions-container");
questionEl = document.getElementById("question");
optionEl = Array.from(document.getElementsByClassName("option"));
highScoreContainer = document.getElementById("your-score");
initials = document.getElementById("initials-input");
const timerEl = document.getElementById("timer");
timesUpCont = document.getElementById("timesUp");
scoreNumHere = document.getElementById("scoreNumGoesHere")
scoreBoardCont = document.getElementById("scoreBoard");
coderInt = document.getElementById("coderInt");
userScore = document.getElementById("coderScore");
highScoresList = document.getElementById("highScoresList");
document.getElementById("option-1").addEventListener("click", function () { checkanswer("1") });
document.getElementById("option-2").addEventListener("click", function () { checkanswer("2") });
document.getElementById("option-3").addEventListener("click", function () { checkanswer("3") });
document.getElementById("option-4").addEventListener("click", function () { checkanswer("4") });
document.getElementById("start-button").addEventListener("click", startGame);
document.getElementById("submit-initials-button").addEventListener("click", saveHighScore);
// one minute
const startingTime = 1;
// 60 seconds
var time = startingTime * 60;
var currentQuestion = {};
var correctOption = true;
var possibleQuestions = [];
var questionIndex = -1;
var timeInterval;
var questionsArr = [
{
question: "Which is not a data type?", option1: "Boolean", option2: "String", option3: "Node", option4: "Array", correctAnswer: "3"
},
{
question: "Which symbol returns the division remainder?", option1: "/", option2: "++", option3: "*", option4: "%", correctAnswer: "4"
},
{
question: "How would you access \"giraffe\" in this array? \: <br> var zoo = [\"zebra\",\"hippo\",\"giraffe\"]", option1: "zoo[3]", option2: "zoo[2]", option3: "zoo[-3]", option4: "zoo[giraffe]", correctAnswer: "2"
},
{
question: "Fixed values are called?", option1: "Literals", option2: "Variables", option3: "Operators", option4: "Arrays", correctAnswer: "1"
}
];
// Game starts when button is pressed (called above)
function startGame() {
timeInterval = setInterval(startTimer, 1000);
insertQuestion();
}
function startTimer() {
if (numOfQuestions = possibleQuestions.length - 1) {
quizStart.classList.add("hide");
}
homePage.classList.add("hide");
quizStart.classList.remove("hide");
const minutes = Math.floor(time / 60);
var seconds = time % 60;
if (seconds < 10) {
seconds = "0" + seconds;
}
if (time <= 0) {
timerEl.innerHTML = "OH NO! Time's Up!";
quizStart.classList.add("hide");
timesUpCont.classList.remove("hide");
}
timerEl.innerHTML = minutes + ":" + seconds;
time--;
}
function insertQuestion() {
possibleQuestions = [...questionsArr];
console.log(possibleQuestions);
loadNextQuestion();
}
function checkanswer(value) {
if (currentQuestion.correctAnswer == value) {
loadNextQuestion();
}else {
time = time - 9;
timerEl.innerHTML ="0:" + time;
loadNextQuestion();
}
}
function loadNextQuestion() {
var numOfQuestions = possibleQuestions.length - 1;
if (questionIndex < numOfQuestions) {
questionIndex += 1;
currentQuestion = possibleQuestions[questionIndex];
question.innerHTML = currentQuestion.question;
answer.innerHTML = currentQuestion.correctAnswer;
optionEl.forEach(optionEl => {
var number = optionEl.dataset['number'];
optionEl.innerText = currentQuestion["option" + number];
});
} else {
highScoresPage();
}
};
function endGame() {
highScoresPage();
}
function highScoresPage() {
quizStart.classList.add("hide");
highScoreContainer.classList.remove("hide");
clearInterval(timeInterval);
var highScore = timerEl;
// console.log(timerEl);
scoreNumHere.innerText ="[" + time + "]";
}
const MAX_HIGHSCORES = 10;
// Got (some) of code below with help of tutor
function saveHighScore() {
var highScoresArr = [];
highScoresArr = JSON.parse(localStorage.getItem("highScores")) || [];
console.log(highScoresArr)
// save initials of user
var int = initials.value;
var score = time;
// packages 1 user's initials with their score(time)
scoreObj = {
int,
score
}
// console.log(scoreObj);
// push each individual user's initials and score to end of array of highscores
highScoresArr.push(scoreObj);
// stringify highscore
localStorage.setItem("highScores", JSON.stringify(highScoresArr));
// console.log(localStorage);
printHighScores(highScoresArr);
function printHighScores(highScoresArr) {
// move to scoreBoard container
highScoreContainer.classList.add("hide");
scoreBoardCont.classList.remove("hide");
// console.log(highScoresArr);
highScoresArr.sort( (a, b) => b.score - a.score)
highScoresArr.splice(10);
// for each object created, make it a list and append
highScoresArr.forEach(scoreObj => {
const listItem = document.createElement('li');
listItem.innerText = `${scoreObj.int} - ${scoreObj.score}`;
listItem.classList.add('highScoresList');
highScoresList.appendChild(listItem);
})
}
}