-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
239 lines (224 loc) · 7.09 KB
/
script.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
'use strict';
// build the question set
const questionSet = [
{
number: 1,
text: `How many broomsticks are flown in<br> a full game of Quidditch?</br>`,
ans1: `Fourteen`,
ans2: `Two hundred and sixty-seven`,
ans3: `Fifteen`,
ans4: `Sixteen`
},
{
number: 2,
text: ` Of which Hogwarts house is Terry Boot a member?`,
ans1: `Slytherin`,
ans2: `Gryffindor `,
ans3: `Hufflepuff `,
ans4: `Ravenclaw`
},
{
number: 3,
text: `Which Polyjuice Potion ingredients must<br> be acquired at the full moon?</br>`,
ans1: `Knotgrass`,
ans2: `A bit of whoever you wish to turn into`,
ans3: `Fluxweed`,
ans4: `Newt spleen`
},
{
number: 4,
text: `What type of creature is an Ashwinder?`,
ans1: `Dog`,
ans2: `Serpent`,
ans3: `Fish`,
ans4: `Bird`
},
{
number: 5,
text: `Which team from the Ministry of Magic is responsible for modifying<br> the memories of Muggles who have been exposed to magic?</br>`,
ans1: `Unspeakables`,
ans2: `Aurors`,
ans3: `The Wizengamot`,
ans4: `Oblivators`
},
{
number: 6,
text: `Where can the Demiguise be found (with difficulty)?`,
ans1: `Russia`,
ans2: `Far East`,
ans3: `North Sea`,
ans4: `South America`
},
{
number: 7,
text: `What house was Moaning Myrtle sorted into?`,
ans1: `Slytherin`,
ans2: `Gryffindor `,
ans3: `Hufflepuff `,
ans4: `Ravenclaw`
},
{
number: 8,
text: `What year was Ollivanders founded?`,
ans1: `465 BC`,
ans2: `382 BC`,
ans3: `57 BC`,
ans4: `712 BC`
},
{
number: 9,
text: `What number is Harry's vault at the Gringotts Wizarding Bank?`,
ans1: `713`,
ans2: `687`,
ans3: `627`,
ans4: `711`
},
{
number: 10,
text: ` Who says this: "I wouldn't touch a filthy little blood<br> traitor like her whatever she looked like."?</br>`,
ans1: `Barty Crouch Jr.`,
ans2: `Marcus Flint `,
ans3: `Lucius Malfoy`,
ans4: `Blaise Zabini`
}
];
// build the answers set
const ANSWERS = [
'Fifteen',
'Ravenclaw',
'Fluxweed',
'Serpent',
'Oblivators',
'Far East',
'Ravenclaw',
'382 BC',
'687',
'Blaise Zabini'
];
//question template
let questionNum = 1;
let rightAnswers = 0 ;
function generateQuestions(rightAnswers, question, questionAnswered) {
return `
<section id="question-page" role="main">
<div id="status-bar"
<span id="question-count">Question:${question.number}/10</span>
<span id ="score-count"> Score: ${rightAnswers}/${questionAnswered}</span>
</div>
<h3 id="question"> ${question.text} </h3>
<form>
<fieldset>
<label for="user answers" class="error">Answers:</label>
<label>
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans1}</span>
</label>
<label>
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans2}</span>
</label>
<label>
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans3}</span>
</label>
<label>
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans4}</span>
</label>
</fieldset>
<button class="btn-hover color-3" id="js-submit-button">Submit</button>
</form>
</section>
`;
}
function startButton() {
$('#js-start-button').click(function(event){
goToNeXT();
});
}
function nextButton() {
$('#container').on('click', '#js-next-button', function(event) {
if(questionNum === 10) {
renderResults();
} else {
iterateQuestion();
goToNeXT();
}
});
}
function goToNeXT() {
const question = questionSet[questionNum -1 ];
const questionAnswered = questionNum - 1;
$('#container').html(generateQuestions(rightAnswers, question, questionAnswered));
}
function submitButton() {
$('#container').on('click', '#js-submit-button', function(event) {
event.preventDefault()
const answer = $('input:checked').siblings('span');
const userRight = checkAnswer(answer);
if (userRight) {
goodFeedback();
} else {
badFeedback();
}
});
}
function checkAnswer(answer) {
if(answer.text() === ANSWERS[questionNum - 1]) {
return true;
} else {
return false;
}
}
function goodFeedback() {
let goodFeedback = `
<section class="feedback-page" role="main">
<h2>Correct!</h2>
<img class="animated" src ="https://media1.tenor.com/images/6f03e32f56fe93eaae38252cbf5cd063/tenor.gif?itemid=10684186">
<p> <button class="btn-hover color-3" id="js-next-button">Next</button> </p>
</section>
`;
$('#container').html(goodFeedback);
iterateRightAnswers();
}
function badFeedback() {
function badFeedbackTemplate(questionNum) {
return `
<section class="feedback-page" role="main">
<h2>Wrong! It was ${ANSWERS[questionNum - 1]}!</h2>
<img src="https://pa1.narvii.com/7008/f31522300d0fc00df564608780a3d90b32aa89d6r1-327-200_hq.gif" alt="It's leviosa">
<p> <button class="btn-hover color-3" id="js-next-button">Next</button></p>
</section>
`;}
$('#container').html(badFeedbackTemplate(questionNum));
}
function iterateRightAnswers() {
rightAnswers++;
}
function iterateQuestion() {
questionNum++;
}
function renderResults () {
if (rightAnswers >= 8) {
$('#container').html(`<div class="results goodFeedback"><h3>A true wizard!</h3><img class=
"animated" src="https://media1.tenor.com/images/0f8588a031f9aaa157df4f519b65180e/tenor.gif?itemid=5453410" alt="You're a wizard harry"/><p>You got ${rightAnswers} / 10</p><p>You've got that magic in you!!</p><button class="btn-hover color-3" type ="restartButton" button id="js-restart-button">Restart Quiz</button></div>`);
} else if (rightAnswers < 8 && rightAnswers >= 5) {
$('#container').html(`<div class="results goodFeedback"><h3>Not that bad! But you can do better!</h3><img class="animated" src="https://media.giphy.com/media/l2JhL6uaJY7WzPbMs/giphy.gif" alt="slow clap Hermione"/><p>You got ${rightAnswers} / 10</p><p>You were so close!</p><button type ="restartButton" button class="btn-hover color-3" id="js-restart-button">Restart Quiz</button></div>`);
} else {
$('#container').html(`<div class="results goodFeedback"><h3>You might want to read the books again </h3><img class ="animated" src="https://media.giphy.com/media/GUhiBgU0DbWsU/giphy.gif" alt="Snape disapointed"/><p>You got ${rightAnswers} / 10</p><p>I'm sure you will do better next time!</p><button class"btn-hover color-3" type ="restartButton" button id="js-restart-button">Restart Quiz</button></div>`);
}
}
function restartQuiz() {
$('#container').on('click', '#js-restart-button', function(event) {
questionNum = 1;
rightAnswers = 0;
goToNeXT();
});
}
function runButtons() {
startButton();
submitButton();
nextButton();
restartQuiz();
}
runButtons();