-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
234 lines (217 loc) · 6.58 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
const store = {
// 5 or more questions are required
questions: [
{
question: 'What can travel around the world while staying in a corner?',
answers: [
'An Email',
'Words',
'A Stamp',
'A Globe'
],
correctAnswer: 'A Stamp',
correctImg: 'images/stamp.jpg'
},
{
question: 'You throw away the outside and cook the inside. Then you eat the outside and throw away the inside. What did you eat?',
answers: [
'Ear of Corn',
'Potato',
'Banana',
'Pistachios'
],
correctAnswer: 'Ear of Corn', // Throw away the husk, cook the kernel, eat the kernel, and throw away the cob
correctImg: 'images/corn.jpg'
},
{
question: 'I feather and run but am not a bird. I bleed but never lived. What am I?',
answers: [
'A River',
'Ink',
'An Arrow',
'Machine'
],
correctAnswer: 'Ink', // Ink can bleed and run, also used on a qwil
correctImg: 'images/ink.jpg'
},
{
question: 'If I have a bee in my hand, what is in my eye?',
answers: [
'Hope',
'A Stinger',
'Beauty',
'Pistachios'
],
correctAnswer: 'Beauty', // (BEAUTY is in the EYE of the bee-holder)
correctImg: 'images/beauty.jpg'
},
{
question: 'You get me when you park in a place off limits. I live in a swamp. I\'m the one who ribbits.',
answers: [
'Frog',
'Parking Ticket',
'Towed',
'Flat Tire'
],
correctAnswer: 'Towed', // Toad
correctImg: 'images/towed.jpg'
}
],
quizStarted: false,
questionNumber: 0,
score: 0,
currentQuestion: 0,
attempts: 0
};
/**
*
* 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 generateIntroHtmlText() {
return $('main').html(
`
<div class="intro">
<h2>Welcome to Riddle Me This</h2>
<form class="intro-form">
<label for="get-started">Shall we get started?</label>
<button type="submit">Lets do it</button>
</form>
</div>
`
)
}
// A function render all the other quiz pages (include argument)
function generateRiddle(currentQuestion) {
return $('main').html(
`
<div class="question-container">
<div class="current-container">
<div class="current-question-num">Question ${store.currentQuestion + 1} out of ${store.questions.length}</div>
<div class="question-score-seperator"> : </div>
<div class="current-score"> Score - ${store.score} out of ${store.questions.length} </div>
</div>
<div class="promt-question">
<div class="current-question">${currentQuestion.question}</div>
<form class="answers-form">
${generateAnswers(currentQuestion)}
</form>
</div>
`
)
}
function generateAnswers(currentQuesion) {
const currentQuestionAnswers = currentQuesion.answers.map(answer => `<button class="answer-buttons">${answer}</button>`)
const currentAnswersString = currentQuestionAnswers.join('');
// console.log(currentAnswersString);
return currentAnswersString;
}
function generateCorrectResult() {
return $('main').html(
`
<div class="answer">Correct! The answer was: <strong>${store.questions[store.currentQuestion].correctAnswer}</strong>
<img src="${store.questions[store.currentQuestion].correctImg}" alt="Image of ${store.questions[store.currentQuestion].correctAnswer}" width="500" height="500">
</div>
<form class="answers-form">
<button class="next-button">Next</button>
</form>
`
)
}
function generateIncorrectResult() {
return $('main').html( `
<div class="answer">Sorry! The correct answer was: <strong>${store.questions[store.currentQuestion].correctAnswer}</strong>
<img src="${store.questions[store.currentQuestion].correctImg}" alt="Image of ${store.questions[store.currentQuestion].correctAnswer}" width="500" height="500">
</div>
<form class="answers-form">
<button class="next-button">Next</button>
</form>
`
)
}
function generateFinalResults() {
return $('main').html( `
<div class="final-results">You scored ${store.score} out of ${store.questions.length}</div>
<form class="answer-form">
<button class="reset-button">Restart Quiz?</button>
</form>
`
)
}
/****************************************/
/********** RENDER FUNCTION(S) **********/
/****************************************/
function startQuiz() {
store.quizStarted = true;
}
function render(selection) {
if (store.quizStarted) {
if (store.attempts === store.questions.length) {
return generateFinalResults();
}
let currentQuestion = store.questions[store.currentQuestion]
let correctAnswer = currentQuestion.correctAnswer;
generateRiddle(currentQuestion);
if (!selection) {
// console.log("Please make a selection");
} else {
if (selection === correctAnswer) {
// console.log("That is correct!")
store.score++
generateCorrectResult();
} else {
// console.log("WRONG!")
generateIncorrectResult();
}
store.attempts++
}
} else {
generateIntroHtmlText();
}
}
/*********************************************/
/********** EVENT HANDLER FUNCTIONS **********/
/*********************************************/
// These functions handle events (submit, click, etc)
function handleQuizBeginsSubmit() {
// when the user clicks the button this is where we handle the beggining
$('.intro-form').on('submit', e => {
e.preventDefault();
startQuiz();
render();
})
}
function checkAnswerSubmit() {
$('main').on('click', '.answer-buttons', e => {
e.preventDefault();
render(e.currentTarget.innerHTML);
})
}
function handleNextQuestonSubmit() {
$('main').on('click', '.next-button', e => {
e.preventDefault();
store.currentQuestion++
render();
})
}
// launch all functions after page loads
function handleQuiz() {
render();
handleQuizBeginsSubmit();
checkAnswerSubmit();
handleNextQuestonSubmit();
}
$(handleQuiz);