-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
201 lines (186 loc) · 6.83 KB
/
app.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
///GLOBAL VARS
const startButton = document.getElementById('startBtn')
const nextButton = document.getElementById('nextBtn')
const highScorebtn = document.getElementById('scorebtn')
const quizContainer = document.getElementById('quiz-Container')
const choiceBtnEL = document.getElementById('choice-buttons')
let randomQuestions, currentQuestionIndex; /// to shuffle the questions
const questionEl = document.getElementById('question-container')//gets the question out of the array of questions
var myTimer = document.getElementById('timer')
let highScore = document.getElementById('highscore')
nextButton.addEventListener('click', () => {
currentQuestionIndex++;
displayNextQuestion();
});
startButton.addEventListener('click', startGame, startTimer)
////--------------------------------------------------------------------------------------------------------------
var time = setInterval(startTimer, 1000);
var score = "";
var scoreArray = JSON.parse(localStorage.getItem("highscores")) || []
//start timer
//-----------------------------------------------------------------------------------------------------------------------------
//The Timer and quiz start functions!!!
function startGame() {
highScorebtn.classList.add('hide')
startButton.classList.add('hide');
quizContainer.classList.remove("hide");
randomQuestions = questions.sort(() => Math.random() - .5);// makes sure i get a
currentQuestionIndex = 0;
displayNextQuestion();
}
//function timer() {
// var seconds = 60;
// var timer = setInterval(function () {
// document.getElementById('secondsLeft')= '00:' + seconds;
// seconds--;
// if (seconds < 0) {
// clearInterval(timer);
// }
//
// }, 1000);
//}
function startTimer(){
timer();
}
function minusScore() {
if (correct != questions[currentQuestionIndex - 1].correct) {
secondsLeft -= 10
}
if (currentQuestionIndex === questions.length) {
score = secondsLeft
localStorage.setItem('highscores', JSON.stringify(score))
location.href - "highscores.html"
}
}
//-----------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------------
function displayQuestion(questions) {
questionEl.innerText = questions.question;
questions.choices.forEach(choice => {
const button = document.createElement('button')
button.innerText = choice.text
button.classList.add('btn')
if (choice.correct) {
button.dataset.correct = choice.correct
}
button.addEventListener('click', selectChoice)
choiceBtnEL.appendChild(button)
})
}
function displayNextQuestion() {
resetQuestion()
displayQuestion(randomQuestions[currentQuestionIndex])
}
function resetQuestion() {
clearStat(document.body)
nextButton.classList.add('hide')
while (choiceBtnEL.firstChild) {
choiceBtnEL.removeChild
(choiceBtnEL.firstChild)
}
}
function selectChoice(e) {
const selectButton = e.target
const correct = selectButton.dataset.correct
setStat(document.body, correct)
Array.from(choiceBtnEL.children).forEach(button => {
setStat(button, button.dataset.correct)
})
if (randomQuestions.length > currentQuestionIndex + 1) {
nextButton.classList.remove('hide')
} else {
startButton.innerText = 'restart'
startButton.classList.remove('hide')
highScorebtn.classList.remove('hide')
}
}
function setStat(element, correct) {
clearStat(element)
if (correct) {
element.classList.add('correct')
}
else {
element.classList.add('wrong')
}
}
function clearStat(element) {
element.classList.remove('correct')
element.classList.remove('wrong')
}
//--------------------------------------------------------------------------------------------------------------------------------
//Question array with choices and correct answer
const questions = [
{
question: "What is the identity operator for not equal?",
choices: [
{ text: 'prompt("Text Here")', correct: false },
{ text: '!==', correct: true },
{ text: 'Placehasdfaolder danswer', correct: false },
{ text: 'Placehasdfaolder danswer', correct: false }
]
}, {
question: "How do you assign a default value to a prompt??",
choices: [
{ text: 'Placehasdfaolder danswer', correct: true },
{ text: 'its a quiz', correct: false },
{ text: 'Placehasdfaolder danswer', correct: false },
{ text: 'Placehasdfaolder danswer', correct: false }
]
}, {
question: "what does ceil() method do?",
choices: [
{ text: 'rounds a number upwards to the nearest interger', correct: true },
{ text: 'red', correct: false },
{ text: 'nothing', correct: false },
{ text: 'lead', correct: false }
]
}, {
question: "Have I gaven up on making questions because it didnt have much time to think of them or answers?",
choices: [
{ text: 'this ', correct: true },
{ text: 'drugs', correct: false },
{ text: 'green', correct: false },
{ text: 'zero', correct: false }
]
}, {
question: "do you hate Tom Cruise",
choices: [
{ text: 'katie holmes does ', correct: true },
{ text: 'Pizza', correct: false },
{ text: 'orangutan', correct: false },
{ text: 'tortuga', correct: false }
]
}, {
question: "Do you like making mee?",
choices: [
{ text: 'its hard', correct: true },
{ text: 'easy', correct: false },
{ text: 'easy', correct: false },
{ text: 'peasy', correct: false }
]
}, {
question: "are dogs the best animal in the world?",
choices: [
{ text: 'beer', correct: true },
{ text: 'yes', correct: false },
{ text: 'no', correct: false },
{ text: 'maybe', correct: false }
]
}, {
question: "whos the tallest man",
choices: [
{ text: 'giraffe', correct: true },
{ text: 'a arrow to the knee', correct: false },
{ text: 'ben stiller', correct: false },
{ text: 'pizza again', correct: false }
]
}, {
question: "whats the biggest problem with nicolas?",
choices: [
{ text: 'keeps biting feet', correct: true },
{ text: 'beats up little kids', correct: false },
{ text: ' doesnt beat up enough little kids', correct: false },
{ text: 'lives with his mom at age 45', correct: false }
]
}
]