-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
305 lines (276 loc) · 7.32 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
/**
* Example store structure
*/
const store = {
// 5 or more questions are required
questions: [
{
question: 'What country is Machu Picchu in?', // #1
answers: [
'Peru',
'Chile',
'Uruguay',
'Venezuela'
],
correctAnswer: 'A',
incorrectComment: 'Peru'
},
{
question: 'What country is Haida Gwaii in??', // #2
answers: [
'Mexico',
'Canada',
'United States',
'Greenland'
],
correctAnswer: 'B',
incorrectComment: 'Canada'
},
{
question: 'What country is Table Mountain in?', // #3
answers: [
'Ghana',
'Kenya',
'South Africa',
'Uganda'
],
correctAnswer: 'C',
incorrectComment: 'South Africa'
},
{
question: 'What country is Paro Taktsang in?', // #4
answers: [
'China',
'Tibet',
'India',
'Bhutan'
],
correctAnswer: 'D',
incorrectComment: 'Bhutan'
},
{
question: 'What country is Santorini in?', // #5
answers: [
'Italy',
'Greece',
'Turkey',
'Malta'
],
correctAnswer: 'B',
incorrectComment: 'Greece'
}
],
quizStarted: false,
questionNumber: 0,
score: 0,
feedbackMessage: ' '
};
/**
*
* Technical requirements:
*
* Your app should include a render() function, that regenerates the view each time the store is updated.
* See your course material and access support for more details.
*
* NO additional HTML elements should be added to the index.html file.
*
* You may add attributes (classes, ids, etc) to the existing HTML elements, or link stylesheets or additional scripts if necessary
*
* SEE BELOW FOR THE CATEGORIES OF THE TYPES OF FUNCTIONS YOU WILL BE CREATING 👇
*
*/
/********** TEMPLATE GENERATION FUNCTIONS **********/
// These functions return HTML templates
function startQuiz() {
store.score = 0;
store.questionNumber = 0;
store.quizStarted = true;
return `
<form id="welcome-screen" class="welcome-screen">
<fieldset>
<p>Welcome to the quiz!<p>
<nav>
<button type="button" id="start-button">Start</button>
</nav>
</fieldset>
<p id="feedback" class="feedback">${store.feedbackMessage}</p>
</form>
`;
}
function generateQuestion() {
let currentQuestion = store.questions[store.questionNumber];
return `
<div class="group">
<div class="item">
${generateImage()}
</div>
<div class="item item-double">
<form id="questionnaire" class="questionnaire">
<fieldset>
<legend>${currentQuestion.question}</legend>
<ol type="A">
${generateAnswers(currentQuestion)}
</ol>
<nav>
<button type="submit" id="submit-button">Submit</button>
<button type="button" id="next-button">Next</button>
</nav>
</fieldset>
<p id="feedback" class="feedback">${store.feedbackMessage}</p>
</form>
</div>
</div>
`;
}
function generateImage(curQue) {
if (store.questionNumber == 0) {
return `
<img src="images/Machu-Picchu.jpg" alt="Ancient Ruins in the Mountains">
`;
} else if (store.questionNumber == 1) {
return `
<img src="images/Gwaii-Haanas.jpg" alt="Trees Overlooking Calm Waters">
`;
} else if (store.questionNumber == 2) {
return `
<img src="images/Table-Mountain.jpg" alt="Flat-topped Mountain">
`;
} else if (store.questionNumber == 3) {
return `
<img src="images/Paro-Taktsang.jpg" alt="Cliff-side Temple Complex">
`;
} else {
return `
<img src="images/Santorini.jpg" alt="Town Overlooking the Sea">
`;
}
}
function generateAnswers(curQue) {
return `
<li>
<input type="radio" id="A" name="answers" value="A" required>
<label for="A">${curQue.answers[0]}</label>
</li>
<li>
<input type="radio" id="B" name="answers" value="B" required>
<label for="B">${curQue.answers[1]}</label>
</li>
<li>
<input type="radio" id="C" name="answers" value="C" required>
<label for="C">${curQue.answers[2]}</label>
</li>
<li>
<input type="radio" id="D" name="answers" value="D" required>
<label for="D">${curQue.answers[3]}</label>
</li>
`;
}
function gradeQuestion(submittedAnswer) {
let currentQuestion = store.questions[store.questionNumber];
if (submittedAnswer == 'right') {
return `
You are correct!
`;
} else {
return `
Incorrect. The answer is ${currentQuestion.incorrectComment}.
`;
}
}
function currentStatus() {
let questionNum = Math.min(store.questionNumber + 1, 5);
return `
<footer>
<div class="status">
<h2>Question: ${questionNum} of ${store.questions.length}</h2>
<div class="score">
<h2>Score: ${store.score} of ${store.questions.length}</h2>
</div>
</div>
</footer>
`;
}
function updateScore() {
$("div.score").replaceWith(`
<div class="score">
<h2>Score: ${store.score} of ${store.questions.length}</h2>
</div>
`);
}
function finishQuiz() {
return `
<form id="denouement" class="denouement">
<fieldset>
<p>Thanks for taking the quiz!<p>
<nav>
<button type="button" id="restart-button">Restart</button>
</nav>
</fieldset>
<p id="feedback" class="feedback">${store.feedbackMessage}</p>
</form>
`;
}
/********** RENDER FUNCTION(S) **********/
// This function conditionally replaces the contents of the <main> tag based on the state of the store
function renderQuiz() {
let htmlCode = '';
if (store.quizStarted == false) {
htmlCode = startQuiz();
$('body').append(currentStatus());
} else if (store.questionNumber < store.questions.length) {
htmlCode = generateQuestion();
} else {
store.quizStarted = false;
htmlCode = finishQuiz();
}
// htmlCode = test();
// htmlCode += currentStatus();
$('main').html(htmlCode);
$('footer').replaceWith(currentStatus());
};
/********** EVENT HANDLER FUNCTIONS **********/
// These functions handle events (submit, click, etc)
function startButton() {
$('body').on('click', '#start-button', function(event) {
renderQuiz();
});
}
function submitButton() {
$('body').on('submit', '#questionnaire', function(event) {
event.preventDefault();
let currentQuestion = store.questions[store.questionNumber];
let selectedAnswer = $('input[name=answers]:checked').val();
if (selectedAnswer == currentQuestion.correctAnswer) {
$('#feedback').append(gradeQuestion('right'));
store.score++;
} else {
$('#feedback').append(gradeQuestion('wrong'));
}
$('#submit-button').hide();
$('input[type=radio]').each(() => {
$('input[type=radio]').attr('disabled', true);
});
$('#next-button').show();
updateScore();
});
}
function nextButton() {
$('body').on('click', '#next-button', function(event) {
store.questionNumber++;
renderQuiz();
});
}
function restartButton() {
$('body').on('click', '#restart-button', function(event) {
$('footer').replaceWith(``);
renderQuiz();
});
}
function main() {
renderQuiz();
startButton();
submitButton();
nextButton();
restartButton();
}
$(main);