-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
229 lines (199 loc) · 6.16 KB
/
script.js
File metadata and controls
229 lines (199 loc) · 6.16 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//DOM Elements
const startButton = document.getElementById('start-button');
const questionCardEl = document.getElementById('question-card');
const questionEl = document.getElementById('question');
const answerButtonsEl = document.getElementById('answer-button');
const nextButton = document.getElementById('next-button');
const timerEl = document.querySelector('.timer-count');
const scoreTextEl = document.querySelector('#score');
const SCORE_POINTS = 100;
const TIME_TO_ANSWER = 20;
const QUESTIONS = [
{
question: 'What is HTML?',
answers: [
{ text: 'Hyper Markup Language', correct: true },
{ text: 'How To Make Lunch', correct: false },
{ text: 'Hamburger with Tomato, Mayo, Lettuce', correct: false },
{ text: 'How To Make Lasagna', correct: false }
]
},
{
question: 'Inside which HTML element do we put the JavaScript?',
answers: [
{ text: '<js>', correct: false },
{ text: '<javascript>', correct: false },
{ text: '<script>', correct: true },
{ text: '<scripting>', correct: false }
]
},
{
question: 'How do you create a function in JavaScript?',
answers: [
{ text: 'function:myFunction()', correct: false },
{ text: 'function/MyFunction', correct: false },
{ text: 'function = myFunction', correct: false },
{ text: 'function myFunction()', correct: true }
]
},
{
question: 'What is one way to add a comment in a JavaScript?',
answers: [
{ text: '"This is a comment', correct: false },
{ text: '//This is a comment', correct: true },
{ text: '<!--This is a comment-->', correct: false },
{ text: '(This is a comment)', correct: false }
]
},
{
question: 'Which operator is used to assign a value to a variable?',
answers: [
{ text: '+', correct: false },
{ text: '-', correct: false },
{ text: '=', correct: true },
{ text: '&', correct: false }
]
},
{
question: 'How do you write "Hello World" in an alert box?',
answers: [
{ text: 'alert("Hello World")', correct: true },
{ text: 'alertBox("Hello World")', correct: false },
{ text: 'msg("Hello World")', correct: false },
{ text: 'boxBox("Hello World")', correct: false }
]
}
];
var shuffledQuestions;
var questionIndex;
var totalScore;
var questionInterval;
var timeLeft;
//Starts the questions and timer
function startQuiz() {
console.log('Start Quiz: Recent Score:', getMostRecentScore());
//Reset Score & Questions & Index
totalScore = 0;
questionIndex = -1;
shuffledQuestions = QUESTIONS.sort(() => Math.random() - .5);
//Tidy Up Display
startButton.classList.add('hide');
questionEl.classList.remove('hide');
questionCardEl.classList.remove('hide');
//Update display
updateLatestScore();
//Populate Question
setNextQuestion();
}
function getMostRecentScore() {
let score = localStorage.getItem('mostRecentScore');
if (score === null) {
return 'Not Played Before';
}
score = parseInt(score, 10);
if (isNaN(score)) {
return 'Invalid Score Saved';
}
return score;
}
function setMostRecentScore(score) {
return localStorage.setItem('mostRecentScore', 0);
}
//sets the next questions and resets
function setNextQuestion() {
resetState();
timeLeft = TIME_TO_ANSWER;
questionIndex++;
startCountdown();
if (questionIndex > shuffledQuestions.length -1) {
quizOver();
} else {
showQuestion(shuffledQuestions[questionIndex]);
}
}
//shows the questions and answers
function showQuestion(question) {
questionEl.innerText = question.question;
question.answers.forEach(answer => {
const button = document.createElement('button');
button.innerText = answer.text;
button.classList.add('button');
if (answer.correct) {
button.dataset.correct = answer.correct;
}
button.addEventListener('click', selectAnswer);
answerButtonsEl.appendChild(button);
});
}
//Starts the timer function
function startCountdown() {
clearInterval(questionInterval);
timerEl.textContent = timeLeft;
questionInterval = setInterval(function() {
timeLeft--;
timerEl.textContent = timeLeft;
if (timeLeft === 0) {
clearInterval(questionInterval);
quizOver();
}
}, 1000);
}
//ends the quiz
function quizOver() {
resetState();
timerEl.textContent = "Quiz Over";
startButton.innerText = "Restart";
startButton.classList.remove('hide');
questionEl.classList.add('hide');
setMostRecentScore(totalScore);
}
function resetState() {
nextButton.classList.add('hide');
while (answerButtonsEl.firstChild) {
answerButtonsEl.removeChild(answerButtonsEl.firstChild);
}
}
//shuffles the quesitons and makes sure the correct answer is choosen
function selectAnswer(e) {
const selectedButton = e.target;
const correct = selectedButton.dataset.correct;
Array.from(answerButtonsEl.children).forEach(button => {
setColorClass(button, button.dataset.correct);
});
if (correct) {
timerEl.innerText = 'Correct!';
totalScore += SCORE_POINTS;
} else {
timerEl.innerText = 'Wrong!';
}
if (shuffledQuestions.length > questionIndex + 1) {
nextButton.classList.remove('hide');
} else {
startButton.innerText = 'Restart';
startButton.classList.remove('hide');
}
clearInterval(questionInterval);
updateLatestScore();
}
function updateLatestScore() {
scoreTextEl.innerText = totalScore;
}
//changes the color the answer if it is correct or wrong
function setColorClass(element, correct) {
clearColorClass(element);
if (correct) {
element.classList.add('correct');
} else {
element.classList.add('wrong');
}
}
//clears the colors for the next questions
function clearColorClass(element) {
element.classList.remove('correct');
element.classList.remove('wrong');
}
//Starts the quiz when clicked
startButton.addEventListener('click', startQuiz)
nextButton.addEventListener('click', () => {
setNextQuestion();
});