-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz.js
68 lines (64 loc) · 1.84 KB
/
quiz.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
/**
* The quiz class can take an abritary length of questions with answers that gives different points.
* It also times answers and provides the results ad hoc.
*
* @author dotnetCarpenter
* @version 0.1
* @classDescription This class creates a new Quiz.
* @return {Quiz} Returns a new Quiz.
* @type {Object}
*/
var Quiz = function() {
var _question = [];
/**
* @method
* @return {Number} The index of the added question
*/
this.addQuestion = function(question) {
return _question.push(question);
};
/**
* @method
* @return {Object} A Question object
*/
this.getQuestion = function(index) {
return _question[index];
};
/**
* @method
* @return {Array} A reference to the internal question array
*/
this.getAllQuestion = function() {
return _question;
}
/* experiment */
// this.length = Array.prototype.length.call(_question);
};
/**
* @method
*/
Quiz.prototype.addQuestions = function(questions) {
if( !Array.isArray(questions) ){ throw new TypeError('The arguments has to be an Array.'); }
var index = 0
, self = this;
questions.forEach(function(el, i, a) {
Ti.API.debug(el);
index = self.addQuestion(el);
Ti.API.debug(self);
console.log('added question #'+i+' of '+a.length+' @ '+index);
});
};
/**
* @method
*
*/
Quiz.prototype.eachPossibility = function(questionIndex, fn) {
//Array.prototype.forEach.call(this.getAllQuestion()[questionIndex].possibilities, fn)
return this.getAllQuestion()[questionIndex].possibilities.forEach(fn);
}
// fix cross debug output TODO: multiple arguments to Ti debug
this.console || (this.console = {}, console.log = Ti.API.debug);
// Check if we're in a CommonJS environment
if( typeof require == 'function' && typeof module == 'object' ) {
exports['quiz'] = Quiz;
}