-
Notifications
You must be signed in to change notification settings - Fork 9
/
quizapp.js
311 lines (246 loc) · 8.81 KB
/
quizapp.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
/**
* Created by jakeclose on 1/16/17.
*/
var currentQuestion;
var categoryID;
var score = 0 ;
var questionsSeen = 0;
var trueAnswer;
var currentCategoryID;
$(function(){
/*
{
categories = [
{
name: "Statistics",
questions: [
{
questionText: "sample question1",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question2",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question3",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question4",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}
]
},
{
name: "Math",
questions: [
{
questionText: "What is 1+1",
answers: ["2", "1", "11", "100"]
},
{
questionText: "what is 7*5",
answers: ["35", "42", "21", "12"]
}
]
},
{
name: "Machine Learning",
questions: [
{
questionText: "sample question1",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question2",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question3",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question4",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}
]
},
{
name: "Biology",
questions: [
{
questionText: "sample question1",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question2",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question3",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
},
{
questionText: "sample question4",
answers: ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}
]
}
];
}
*/
categories = [{
"name": "Statistics",
"questions": [{
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}, {
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}, {
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}, {
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}]
},
{
"name": "ML",
"questions": [{
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}, {
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}, {
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}, {
"questionText": "sample question1",
"answers": ["correctanswer", "wronganswer1", "wronganswer2", "wronganswer3"]
}]
},
];
//id for the start menu and category menu
var startMenu = document.getElementById('myModal');
var categoryMenu = document.getElementById('categoryPicker');
var categoryList = document.getElementsByClassName('categories');
$(startMenu).modal('show').on("hidden", function () {
displayCategory(categoryMenu, categoryList);
showCategoryQuestion(categoryMenu);
});
$(".submitButton").click(function () {
questionsSeen++;
var answer = $("input:radio[name=answerBTN]:checked").val();
if( answer == trueAnswer){
console.log("Correct Answer");
score++;
}
var answersList = $(document).find(".UI > .answers");
$(answersList).empty();
$(categoryMenu).modal('show');
});
$( ".endQuiz" ).click(function () {
displayScore(score, questionsSeen);
});
});
function displayCategory(categoryMenu, categoryList) {
$(categoryMenu).modal('show');
for (i = 0; i < categories.length; i++) {
// choice = categories[categoryID].questions[currentQuestion].answers[i];
choice = categories[i].name;
$('<li><input type="radio" value=' + i + ' name="categoryBTN" />' + choice + '</li>').appendTo(categoryList);
}
}
function showCategoryQuestion(categoryMenu) {
$(categoryMenu).on("hidden", function () {
currentCategoryID = parseInt($("input:radio[name=categoryBTN]:checked").val());
if(currentCategoryID < categories.length) {
getRandomQuestion(currentCategoryID);
}
});
}
//Given a cateogory ID, this function displays a random question from that category
function getRandomQuestion(id){
// determine the number of questions in the current category
var numQuestions = categories[id].questions.length;
// create a random question id that is smaller than the number of questions
var randomID = Math.floor((Math.random() * (numQuestions)));
//set the categoryid and current question to the one were currently looking at
categoryID = id;
currentQuestion = randomID;
//sanity checks
if(randomID < 0 || currentQuestion < 0){
throw error;
}
//display the question
displayQuestion(id, randomID);
}
//Give a categoryid and questionid, this function displays a question.
function displayQuestion(categoryID, currentQuestion ) {
if(categoryID < 0 || currentQuestion < 0){
throw error;
}
//select the category name and add it the category section
var category = categories[categoryID].name;
var categoryClass = $(document).find(".UI > .category");
//Throw error if category has no name
$(categoryClass).text(category);
//select the question text and add it question section
var question = categories[categoryID].questions[currentQuestion].questionText;
// console.log(question);
var questionClass = $(document).find(".UI > .questionText");
$(questionClass).text(question);
//determine the number of answers
var numChoices = categories[categoryID].questions[currentQuestion].answers.length;
var answersList = $(document).find(".UI > .answers");
var answerIndex = 0;
var answersArray = categories[categoryID].questions[currentQuestion].answers;
answersArray = shuffle(answersArray, answerIndex);
//go through the possible number of answers and create a answer box
var choice;
for (i = 0; i < numChoices; i++) {
// choice = categories[categoryID].questions[currentQuestion].answers[i];
choice = answersArray[i];
$('<li><input type="radio" value=' + i + ' name="answerBTN" />' + choice + '</li>').appendTo(answersList);
}
}
//Fisher-Yates (aka Knuth) Shuffle.
function shuffle(array, answerIndex) {
var currentIndex = array.length, temporaryValue, randomIndex;
var correctAnswerSeen = false;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
//If the question to moved is the first index (the real answer), and it is the first time it is moved, set that true answerIndex to
//the current Index to be changed and set the bool to true
if(randomIndex == 0 && correctAnswerSeen == false){
trueAnswer = currentIndex;
correctAnswerSeen = true;
}
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function displayScore(score, questionsSeen) {
var scoreboard = document.getElementById('scoreboard');
$(scoreboard).modal("show");
var percentage = document.getElementsByClassName("score");
var scoreHead = document.getElementsByClassName("scoreHeader");
//Throw error if category has no name
if(questionsSeen == 0){
$(percentage).text("Now Questions Answered");
}
else{
$(scoreHead).text("You answered " + score + " out of " + questionsSeen + " questions ");
var finalScore = (score/questionsSeen) * 100 ;
$(percentage).text("For a final score of " + finalScore);
}
}