-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
319 lines (284 loc) · 8.29 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
const store = {
// 5 or more questions are required
questions: [
{
question: 'The Wankel engine is also known as...',
answers: [
'the inline 6 engine',
'the rotary engine',
'the boxer engine',
'the radial engine'
],
correctAnswer: 'the rotary engine'
},
{
question: `What did Mazda start off producing?`,
answers: [
'Cars, duh',
'Steam-powered trains',
'Cork',
'Soap'
],
correctAnswer: 'Cork'
},
{
question: 'What is the BMW logo reflective of?',
answers: [
'The crash dummy',
'The household fan',
'The propellers on early planes',
'Their cars drifting donuts'
],
correctAnswer: 'The propellers on early planes'
},
{
question: 'The Volkswagen Group own all of the following except...',
answers: [
'Porsche',
'Audi',
'Lamborghini',
'Volvo'
],
correctAnswer: 'Volvo'
},
{
question: 'In 2015, which car manufacturer was found guilty of a major emissions scandal?',
answers: [
'Subaru',
'Volkswagen',
'BMW',
'Hyundai'
],
correctAnswer: 'Volkswagen'
},
{
question: 'Which grade of fuel does not exist at most gas stations?',
answers: [
'85 octane',
'87 octane',
'89 octane',
'91 octane'
],
correctAnswer: '85 octane'
},
{
question: 'You are least likely to be able to drift with which of the following powertrains?',
answers: [
'Rear-wheel drive',
'Front-wheel drive',
'4x4',
'All-wheel drive'
],
correctAnswer: 'Front-wheel drive'
},
{
question: 'In a manual (stickshift) vehicle, what happens if you turn the key while stepping on the third pedal?',
answers: [
'The battery shorts out',
'The car turns on',
'The alarm starts screaming',
'Your car takes a screenshot'
],
correctAnswer: 'The car turns on'
},
{
question: 'ABS has been mandated on cars since 2012 in the US. It stands for...',
answers: [
'Airbag Blowout System',
'Alarm for Battery Shortage',
'Arnold Brunswick Shaft',
'Anti-lock Brake System'
],
correctAnswer: 'Anti-lock Brake System'
},
{
question: 'Which of the following has the fastest 0 - 60 mph time? (Among the selection below.)',
answers: [
'Lamborghini Huracan Performante',
'Toyota Prius GT',
'Tesla Model S Plaid',
'Nissan GT-R Nismo'
],
correctAnswer: 'Tesla Model S Plaid'
},
],
quizStarted: false,
questionNumber: 0,
score: 0
};
/********** TEMPLATE GENERATION FUNCTIONS **********/
// These functions return HTML templates
let num = 0;
let correctAnsNum = 0;
const currentCode = [];
function statusKeeper() {
console.log('initializing question and score counters')
return `
<div class='question-score-number'>
<p>Question Number: ${store.questionNumber}/10</p>
<p>Score: ${store.score}/100</p>
</div>`;
}
function initialScreen() {
console.log('initializing initial screen')
return `
<div class='home-screen'>
<p>Welcome! This quiz will test you on how much you know about things in the automotive world. Click "BEGIN" when ready.</p>
</div>
<button class='begin-button'>BEGIN</button>`;
}
function questionScreens() {
console.log('initializing question screens')
return `
<div class='questions'>
<form id="question-form">
<fieldset>
<div class='question'>
<legend>${store.questions[num].question}</legend>
</div>
<div class='options'>
<div class='option-container-0'>
<input type='radio' name='answer-choice' id='ans-1' value='0' required>
<label for='ans-1'> ${store.questions[num].answers[0]}</label>
</div>
<div class='option-container-1'>
<input type='radio' name='answer-choice' id='ans-2' value='1' required>
<label for='ans-2'> ${store.questions[num].answers[1]}</label>
</div>
<div class='option-container-2'>
<input type='radio' name='answer-choice' id='ans-3' value='2' required>
<label for='ans-3'> ${store.questions[num].answers[2]}</label>
</div>
<div class='option-container-3'>
<input type='radio' name='answer-choice' id='ans-4' value='3' required>
<label for='ans-4'> ${store.questions[num].answers[3]}</label>
</div>
</div>
</fieldset>
<button type="submit" class='submit-button' tabindex='5'>Submit</button>
</form>
</div>`;
}
function incorrectMessage() {
console.log('initializing message for wrong answer');
return `
<div class='incorrect-answer'>
<p>Whoops, the right answer is actually '${store.questions[num].correctAnswer}'.</p>
</div>
<button class='next-button'>Next</button>`;
}
function correctMessage() {
console.log('initializing message for right answer');
return `
<div class='correct-answer'
<p>You are correct! You get 10 points.</p>
</div>
<button class='next-button'>Next</button>`;
}
function finalScreen() {
console.log('initializing final screen');
return `
<div class='last-screen'>
<p>Congrats, you finished!<br>
Your final score is: ${store.score}!</p>
</div>
<button class='restart'>Restart Quiz</button>`;
}
/********** RENDER FUNCTION(S) **********/
// This function conditionally replaces the contents of the <main> tag based on the state of the store
function renderInitialScreen () {;
currentCode.push(initialScreen())
}
function renderQuestionScreen() {
console.log('starting render of question screen');
currentCode.push(questionScreens())
}
function renderFinalScreen() {
console.log('starting render of last screen');
currentCode.push(finalScreen());
}
function renderScores() {
currentCode.push(statusKeeper());
}
function renderCorrectMessage() {
console.log('user was correct');
currentCode.push(correctMessage());
return $('.main-screen').html(currentCode);
}
function renderWrongMessage() {
console.log('user was wrong');
currentCode.push(incorrectMessage());
return $('.main-screen').html(currentCode);
}
function renderMain() {
if (store.quizStarted == false) {
renderInitialScreen();
}
else if (store.quizStarted === true && store.questionNumber > 10) {
renderFinalScreen();
}
else {
renderScores();
renderQuestionScreen();
}
return $('main').html(currentCode);
}
/********** EVENT HANDLER FUNCTIONS **********/
// These functions handle events (submit, click, etc)
function handleBegin() {
$('.main-screen').on('click', '.begin-button', function(event) {
console.log(`'handleBegin' ran`);
store.quizStarted = true;
store.questionNumber += 1;
currentCode.splice(0,1)
renderMain();
})
}
function handleQuestion() {
$('.main-screen').submit(function(event) {
event.preventDefault();
console.log(`'handleQuestion' ran`);
currentCode.splice(1,2);
if (checkResponse() === true) {
store.score += 10;
renderCorrectMessage();
}
else {
renderWrongMessage();
}
});
}
function handleTransition() {
$('.main-screen').on('click', '.next-button', function(event) {
store.questionNumber += 1;
currentCode.splice(0,currentCode.length);
num += 1;
renderMain();
});
}
function handleReset() {
$('.main-screen').on('click', '.restart', function(event) {
console.log('initiating reset');
store.questionNumber = 0;
store.score = 0;
store.quizStarted = false;
num = 0;
currentCode.splice(0,currentCode.length);
renderMain();
});
}
function checkResponse() {
console.log('handler for response checking initiated');
correctAnsNum = store.questions[num].answers.findIndex(item => item === store.questions[num].correctAnswer);
const choices = document.getElementsByName('answer-choice');
return document.getElementsByName('answer-choice')[correctAnsNum].checked;
}
function handleController() {
renderMain();
handleBegin();
handleQuestion();
handleTransition();
handleReset();
}
$(handleController);