-
Notifications
You must be signed in to change notification settings - Fork 0
/
quiz.js
147 lines (118 loc) · 4.9 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
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
Parse.initialize("juAtaRefP6h38DGNrixK5VSeUajQtBhmvpAB8E4T", "9Tl65yTOEWHZyPuGfD103Qe8yU2RmfmX5fUVm8F0")
//var TestObject = Parse.Object.extend("TestObject");
//var testObject = new TestObject();
//testObject.save({foo: "bar"}, {
// success: function(object) {
// alert("yay! it worked");
// }
//});
var quiz = (function(){
var exports = {}
var useLocal= true;
if (useLocal) {
if (localStorage.cQI==undefined){
var score = 0//student's current score
var currentQuestionIndex = 0; // student's current question
}
else {
var score = localStorage.score
var currentQuestionIndex = localStorage.qCI
}
}
if(!useLocal){
var progress = Parse.Object.extend("progress");
var query = new Parse.Query(progress)
if (query.find().length == 0){
var myProg = new progress
myProg.set("score", 0).set("cQI", 0)
}
else{
var myProg = query.find()[0]
}
}
var questions = [{'questionText': 'Sam thinks y = 2x goes to _____ as x goes from 1 to 10', options:['increses', 'decreases', '+ then -', '- then +'], 'solnIndex':0},
{'questionText': 'Jack thinks y = -2x goes to _____ as x goes from 1 to 10', options:['increses', 'decreases', '+ then -', '- then +'], 'solnIndex':1}];//contains questionText, solutions, options
var answers= [];//Student's input answers
var question = questions[currentQuestionIndex]
var checkAnswer = (function(ans){
// console.log('checked');
// var helpMe = question.solnIndex
// var checker = helpMe == ans
// return checker
var req = $.ajax({
url:"http://localhost:8080/",
data: {id : 10, 'answer':ans, 'cQI':currentQuestionIndex}}).done(function(msg){
console.log(msg);
return(msg)
});
return req;
});
//Compares student's answer to correct answer
var getAnswers = (function(){
var answer= $('input[name="option' + currentQuestionIndex + '"]:checked').val();
bool = checkAnswer(answer);
if (bool){
incrementScore();
console.log(score);
}
questionDone(checkAnswer(answer))
})
var questionDone = (function(bool) {
$('.checkAnswer').off('click').text("Next Question").on("click", displayQuestion)
currentQuestionIndex += 1;
localStorage.cQI = currentQuestionIndex
localStorage.score = score
if (bool) {
$('.quiz').append('Correct! Score:'+score);
}
else {
$('.quiz').append('Wrong ): Score:'+score);
}
});
var displayQuestion = (function(){
if (currentQuestionIndex<questions.length){
console.log('Question displayed')
$('.quiz').empty()
question = questions[currentQuestionIndex]
var questionText = $("<div class = 'questiontext'>" + (currentQuestionIndex+1)+ ':'+ questions[currentQuestionIndex].questionText +"</div>")
var options = $('<div></div>')
for (var i=0;i<questions[currentQuestionIndex].options.length;i++){
var option = $('<div>', {class:'option'})
var radio= $("<input>",{type: "radio",
name: "option" + currentQuestionIndex,
value: i});
option.append(radio , questions[currentQuestionIndex].options[i])
options.append(option)
}
var checkAnswerButton = $('<button>', {class:'checkAnswer',
text:'Check Answer'})
$('.quiz').append(questionText)
$('.quiz').append(options)
$('.quiz').append(checkAnswerButton)
$('.checkAnswer').on('click' , getAnswers)
}
//if there are questions left, display the next question.
else{
$('.quiz').empty().append("Quize Finshed! Final Score:" + localStorage.score + "/" + questions.length)
var tryAgain = $('<button>', {class:"tryAgain",
text:"Try Again"
})
$('.quiz').append(tryAgain)
tryAgain.on('click', (function(){
localStorage.clear()
$('.quiz').empty().append("Refresh and you're ready to go!")
}));
}
});//Builds html to display the question
var incrementScore = (function(){
score++;
});//Adds to score
var setup = (function(){
displayQuestion();
});//Called at runtime
exports.setup = setup
return exports
})();
$(document).ready(function(){
quiz.setup()
});