-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion.js
More file actions
282 lines (216 loc) · 6.28 KB
/
question.js
File metadata and controls
282 lines (216 loc) · 6.28 KB
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
var Question = function(id, bodyText, answerText, image) {
// the question's id
this.id = id;
// the question body text.
this.bodyText = bodyText;
// a text representation of the answer. used for debugging. do not compare
// with supplied answer for testing user input
this.answerText = answerText;
this.modelAnswer = "this is a model answer. PLEASE FIX ME";
// the type of question . eg multi, numebr, text
this.questionType;
// checks some supplied answer against the correct answer. returns bool
this.checkAnswer;
// function to convert a question to JSON format (may be unnecessary))
this.toJSON;
//the url of the image to display. leave blank if you want no picture
this.imageURL=image;
this.isNumeric =function(answer) {
return (answer >=0 || answer < 0);
}
this.isEmpty=function(answer){
return answer=="";
}
//funcftion to conver from JSON format to question
// this.fromJSON;
};
/**
* A multi choice question that lets users select from the provided choices
* (optionsText). The index of the correct option is provided too
*/
var MultiChoiceQuestion = function(id, bodyText, answerIndex, optionsText, image) {
// call the super class constructor
Question.call(this, id, bodyText, optionsText[answerIndex], image);
for(var i=0;i<optionsText;i++){
optionsText[i] = optionsText[i].trim();
}
this.optionsText = optionsText;
this.answerIndex = answerIndex;
this.questionType = "multi";
/**
* Checks the answer against the supplied INDEX
*/
this.checkAnswer = function(answer) {
if(answer==-1){
throw new Error("Incorrect format: empty field");
}
if (this.answerIndex == answer) {
// alert("Correct!");
return true;
} else {
// alert("Wrong answer.");
return false;
}
};
this.toJSON = function() {
var jobject = {};
jobject["id"] = id;
jobject["bodyText"] = bodyText;
jobject["optionsText"] = optionsText;
jobject["answerIndex"] = answerIndex;
jobject["image"]=image;
return jobject;
}
};
/**
* A question which asks the user for a number.
// */
// var NumberEntryQuestion = function(id, bodyText, answerNum, image) {
// Question.call(this, id, bodyText, answerNum, image);
// this.questionType = "number";
// this.answerText = Number(answerNum);
// function isNumeric(num) {
// return (num >=0 || num < 0);
// }
// /**
// * compares the supplied number with the correct answer. Currently has no
// * tolerance, answers must be exact
// */
// this.checkAnswer = function(answer) {
// if(!isNumeric(answer)){
// throw "Incorrect format: not a number";
// }
// answer = parseAnswer(answer);
// if (Number(answer) == NaN) {
// // alert("Only numbers allowed.");
// return false;
// } else if (this.answerText == answer) {
// // alert("Correct!");
// return true;
// } else {
// // alert("Wrong answer.");
// return false;
// }
// };
// this.toJSON = function() {
// var jobject = {};
// jobject["id"] = id;
// jobject["bodyText"] = bodyText;
// jobject["answerNum"] = answerNum;
// jobject["image"]=image;
// return jobject;
// };
// // this.fromJSON = function(jobject){
// // this.answer = jobject["canswer"];
// // this.body = jobject["body"];
// // }
// };
/**
* Just like a Number Entry Question but with a tolerance set for it.
*/
var NumberEntryToleranceQuestion = function(id, bodyText, answerNum, tolerance, image) {
Question.call(this, id, bodyText, answerNum, image);
this.questionType = "number";
this.tolerance = tolerance;
this.checkAnswer = function(answer) {
answer = parseAnswer(answer);
if(this.isEmpty(answer)){
throw new Error("Incorrect format: empty field");
}
if(!this.isNumeric(answer)){
throw new Error("Incorrect format: not a number");
}
answer = parseFloat(answer);
answerNum = parseFloat(answerNum);
tolerance = parseFloat(tolerance);
if (answer == NaN) {
// alert("Only numbers allowed.");
return false;
} else if (Math.abs(answer-answerNum) <= tolerance) {
// alert("Correct!");
return true;
}
else {
// alert("Wrong answer.");
return false;
}
};
this.toJSON = function() {
var jobject = {};
jobject["id"] = id;
jobject["bodyText"] = bodyText;
jobject["answerNum"] = answerNum;
jobject["image"]=image;
return jobject;
};
};
/**
* Text question where users must enter an answer in string form.
*/
var TextEntryQuestion = function(id, bodyText, answersArray, image) {
Question.call(this, id, bodyText, answersArray, image);
this.questionType = "text";
this.answersArray = answersArray;
/**
* Checks the array of possible answers for the supplied text
*/
this.checkAnswer = function(answer) {
answer = parseAnswer(answer);
if(this.isEmpty(answer)){
throw new Error("Incorrect format: empty field");
}
if(this.isNumeric(answer)){
throw new Error("Incorrect format: not a string");
}
if (answersArray.indexOf(answer) != -1) {
// alert("Correct!");
return true;
} else {
// alert("Wrong answer.");
return false;
}
};
this.toJSON = function() {
var jobject = {};
jobject["id"] = id;
jobject["bodyText"] = bodyText;
jobject["answersArray"] = answersArray;
jobject["image"]=image;
return jobject;
};
};
/**
* Test method for creating a series of questions
*/
function dummyQuestions() {
MultiChoiceQuestion.prototype = Object.create(Question.prototype);
NumberEntryQuestion.prototype = Object.create(Question.prototype);
TextEntryQuestion.prototype = Object.create(Question.prototype);
var mcq = new MultiChoiceQuestion("NoID",
"Is this operation 'true' or 'false': \n 3 > 2", 0, [ "true",
"false" ]);
var neq = new NumberEntryQuestion("NoID", "How many legs does a human being have?",
2);
var acceptableAnswers = [ "feline", "cat", "hamster", "orange dog" ];
var teq = new TextEntryQuestion("NoID", "what is an orange furry pet",
acceptableAnswers);
var threeQuestions = [ mcq, neq, teq ];
for (var i = 0; i < threeQuestions.length; i++) {
// q.askQuesiton();
threeQuestions[i].checkAnswer("orange dog");
}
};
/**
*This should remove white spaces in user input
*This makes it all small case
*
*Input: user's answer
*Output: parsed string
*/
function parseAnswer(userAnswer){
var maniText = userAnswer;
maniText = maniText.toLowerCase();
maniText = maniText.trim();
var finalInput = maniText;
return finalInput;
};