-
Notifications
You must be signed in to change notification settings - Fork 8
/
maths.html
380 lines (353 loc) · 14.5 KB
/
maths.html
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Simple maths quiz to teach Basic Facts at primary school level">
<meta name="author" content="Steve Pavarno: www.unicycle.co.nz">
<title>Maths Game</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<style>
body {
background-color: #f0f0f0;
}
.bordered {
background-color: white;
border: 1px solid #ddd;
padding: 20px;
border-radius: 5px;
}
.answers-box {
margin-top: 20px;
}
.answer {
float: left;
width: 8em;
border: 1px solid #ddd;
padding: 5px;
margin: 5px;
border-radius: 5px;
}
.right {
border-color: #5cb85c;
color: #5cb85c;
background-color: #DBFFDB;
}
.wrong {
border-color: #d9534f;
color: #d9534f;
background-color: #FFD2CE;
}
</style>
</head>
<body>
<div class="container-fluid">
<h1>Maths Game</h1>
<form class="form-inline" role="form">
<p>
<label for="minutes">Minutes</label>
<select id="minutes" name="minutes">
<option>2</option>
<option selected>3</option>
<option>5</option>
</select>
<label for="questions">Questions</label>
<select id="questions" name="questions">
<option>10</option>
<option>50</option>
<option selected>100</option>
<option>200</option>
</select>
<label for="left_range">Difficulty</label>
<select id="left_range" name="left_range">
<option value="1">0 to 1</option>
<option value="2">0 to 2</option>
<option value="3">0 to 3</option>
<option value="4">0 to 4</option>
<option value="5" selected>0 to 5</option>
<option value="6">0 to 6</option>
<option value="7">0 to 7</option>
<option value="8">0 to 8</option>
<option value="9">0 to 9</option>
<option value="10">0 to 10</option>
<option value="11">0 to 11</option>
<option value="12">0 to 12</option>
<option value="20">0 to 20</option>
<option value="50">0 to 50</option>
<option value="100">0 to 100</option>
</select>
<select id="operator_type" name="operator_type">
<option selected>+</option>
<option>-</option>
<option>x</option>
<option>÷</option>
</select>
<select id="right_range" name="right_range">
<option value="1">0 to 1</option>
<option value="2">0 to 2</option>
<option value="3">0 to 3</option>
<option value="4">0 to 4</option>
<option value="5" selected>0 to 5</option>
<option value="6">0 to 6</option>
<option value="7">0 to 7</option>
<option value="8">0 to 8</option>
<option value="9">0 to 9</option>
<option value="10">0 to 10</option>
<option value="11">0 to 11</option>
<option value="12">0 to 12</option>
<option value="20">0 to 20</option>
<option value="50">0 to 50</option>
<option value="100">0 to 100</option>
</select>
</p>
<div class="bordered">
<p class="text-center clearfix lead">
<label for="answer"><span id="left">99</span>
<span id="operator_value">+</span>
<span id="right">99</span> = </label>
<input type="text" id="answer" size="5" width="7" autofocus class="form-control input-lg" title="Type your answer here and press the Enter key on your keyboard">
<span class="pull-right">
<button type="button" id="start" class="btn btn-success btn-lg">Start</button>
<button type="button" id="stop" class="btn btn-danger btn-lg" disabled>Stop</button>
</span>
</p>
<div>Time <span id="seconds_remaining_text"></span>
<div class="progress" title="Fills up as time passes">
<div class="progress-bar" id="seconds_remaining_bar" role="progressbar">
</div>
</div>
</div>
<div>Questions Asked <span id="questions_asked_text"></span>
<div class="progress" title="Fills up as you answer each question">
<div class="progress-bar" id="questions_asked_bar" role="progressbar">
</div>
</div>
</div>
<div>Correct Answers <span id="questions_correct_text"></span>
<div class="progress" title="Fills up for each correct answer you make">
<div class="progress-bar progress-bar-success" id="questions_correct_bar" role="progressbar">
</div>
</div>
</div>
<p>
</p>
</div>
</form>
</div>
<p></p>
<div class="container-fluid answers-box">
<p>Your answers</p>
<div class="bordered clearfix" id="answers">
</div>
</div>
<div class="container-fluid answers-box" id="completed_image">
</div>
<!-- /.container -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script>
var quiz = {
timeoutID: 0, // from setTimeout()
secondsAllowed: 0,
secondsRemaining: 0,
questionsAsked: 0,
questionsCorrect: 0,
questionsAllowed: 0,
leftRange: 10,
rightRange: 10,
operator: '+',
getLeftRange: function() {
return parseInt(jQuery("#left_range option:selected").val());
},
getRightRange: function() {
return parseInt(jQuery("#right_range option:selected").val());
},
getOperator: function() {
return jQuery("#operator_type option:selected").val();
},
start: function () {
jQuery('#start').attr('disabled', 'disabled');
jQuery('#stop').removeAttr('disabled');
if (quiz.secondsRemaining > 0) return;
quiz.secondsAllowed = parseInt(jQuery("#minutes option:selected").val()) * 60;
quiz.questionsAllowed = parseInt(jQuery("#questions option:selected").val());
quiz.leftRange = quiz.getLeftRange();
quiz.rightRange = quiz.getRightRange();
quiz.operator = quiz.getOperator();
quiz.secondsRemaining = quiz.secondsAllowed + 1;
quiz.questionsCorrect = 0;
quiz.questionsAsked = 0;
quiz.hideImage();
quiz.clearAnswers();
quiz.askQuestion();
quiz.tick();
},
tick: function() {
console.log( 'tick', quiz.secondsRemaining );
if (quiz.secondsRemaining <= 0) {
quiz.timeoutID = 0;
quiz.displaySecondsRemaining(0);
return;
}
quiz.secondsRemaining--;
quiz.displaySecondsRemaining(quiz.secondsRemaining);
quiz.timeoutID = window.setTimeout(quiz.tick, 1000);
},
stop: function() {
quiz.displayQuestionsAsked();
quiz.displayQuestionsCorrect();
quiz.secondsRemaining = 0;
quiz.questionsAllowed = 0;
if (quiz.timeoutID) {
window.clearTimeout(this.timeoutID);
quiz.timeoutID = 0;
}
jQuery('#right').text('_');
jQuery('#left').text('_');
jQuery('#start').removeAttr('disabled');
jQuery('#stop').attr('disabled', 'disabled');
quiz.showImage();
},
displaySecondsRemaining: function(seconds) {
seconds = Math.min(seconds, quiz.secondsAllowed);
if (seconds <= 0) {
jQuery('#seconds_remaining_bar').css('width', 0);
jQuery('#seconds_remaining_text').text('');
} else {
jQuery('#seconds_remaining_bar').css('width', (quiz.secondsAllowed - seconds) / quiz.secondsAllowed * 100 + '%');
jQuery('#seconds_remaining_text').text(Math.floor(seconds / 60) + ' minutes ' + (seconds % 60) + ' seconds left');
}
},
displayQuestionsAsked: function() {
if (quiz.questionsAsked <= 0) {
jQuery('#questions_asked_bar').css('width', 0);
jQuery('#questions_asked_text').text('');
} else if (quiz.questionsAllowed <= 0) {
// using it without the timer
jQuery('#questions_asked_bar').css('width', '100%');
jQuery('#questions_asked_text').text(quiz.questionsAsked);
} else {
jQuery('#questions_asked_bar').css('width', Math.min(quiz.questionsAsked,quiz.questionsAllowed) / quiz.questionsAllowed * 100 + '%');
jQuery('#questions_asked_text').text(quiz.questionsAsked);
}
},
displayQuestionsCorrect: function() {
if (quiz.questionsCorrect <= 0) {
jQuery('#questions_correct_bar').css('width', 0);
jQuery('#questions_correct_text').text('');
} else if (quiz.questionsAllowed <= 0) {
// using it without the timer
jQuery('#questions_correct_bar').css('width', quiz.questionsCorrect / quiz.questionsAsked * 100 + '%');
jQuery('#questions_correct_text').text(quiz.questionsCorrect + " out of " + quiz.questionsAsked);
} else {
jQuery('#questions_correct_bar').css('width', quiz.questionsCorrect / quiz.questionsAllowed * 100 + '%');
jQuery('#questions_correct_text').text(quiz.questionsCorrect + " out of " + quiz.questionsAsked);
}
},
askQuestion: function() {
if (quiz.questionsAsked >= quiz.questionsAllowed && quiz.timeoutID) {
// if we are in timed mode, stop the timer
quiz.stop();
return;
}
quiz.questionsAsked++;
var left = Math.floor(Math.random() * (quiz.leftRange + 1));
var right = Math.floor(Math.random() * (quiz.rightRange + 1));
jQuery('#answer').focus();
jQuery('#right').text(left);
jQuery('#left').text(right);
quiz.displayQuestionsAsked();
quiz.displayQuestionsCorrect();
},
checkAnswer: function(answer) {
var right = parseInt(jQuery('#right').text());
var left = parseInt(jQuery('#left').text());
var correct = 0;
switch (quiz.operator) {
case '+': correct =left + right; break;
case '-': correct =left - right; break;
case 'x': correct =left * right; break;
default: if (right > 0) correct = Math.floor(left / right); else correct = 0; break;
}
if (isNaN(answer)) answer = '?';
var text = left + quiz.operator + right + '=' + answer;
if (answer == correct) {
quiz.addCorrectAnswer(text);
} else {
quiz.addWrongAnswer(text);
}
quiz.askQuestion();
},
clearAnswers: function() {
jQuery('#answers').text('');
},
addCorrectAnswer: function(text) {
quiz.questionsCorrect++;
jQuery('#answers').append('<div class="answer right">' + text + ' <span class="glyphicon glyphicon-ok"></span></div>');
},
addWrongAnswer: function(text) {
jQuery('#answers').append('<div class="answer wrong">' + text + ' <span class="glyphicon glyphicon-remove"></span></div>');
},
keyPress: function(event) {
if ( event.which == 13 ) {
// enter key lets us check the answer
var value = jQuery('#answer').val();
if (value != '') {
quiz.checkAnswer(parseInt(value));
}
jQuery('#answer').val('');
event.preventDefault();
return false;
}
if (event.which == 8 || event.which == 46) {
// allow backspace
return true;
}
if (event.which == 45) {
// allow minus sign
return true;
}
if (event.which < 48 || event.which > 57) {
// discard non-digits
event.preventDefault();
return false;
}
},
hideImage: function() {
jQuery('#completed_image').html('');
},
showImage: function() {
jQuery('#completed_image').html('<div class="bordered"><h1>Well done!</h1><p>Here is a random cat to brighten your day</p><img src="http://thecatapi.com/api/images/get?format=src&time=' + Date.now() + '" style="max-width:100%"></div>');
}
};
function setLeftRange() {
var value = quiz.getLeftRange();
quiz.leftRange = value;
jQuery('#left').text(value);
}
function setRightRange() {
var value = quiz.getRightRange();
quiz.rightRange = value;
jQuery('#right').text(value);
}
function setOperator() {
var value = quiz.getOperator();
quiz.operator = value;
jQuery('#operator_value').text(value);
}
quiz.hideImage();
jQuery('#start').click(quiz.start);
jQuery('#stop').click(quiz.stop);
jQuery('#left_range').change( setLeftRange );
jQuery('#operator_type').change( setOperator );
jQuery('#right_range').change( setRightRange );
jQuery('#answer').keypress(quiz.keyPress);
// set default values on startup
setLeftRange();
setRightRange();
setOperator();
</script>
</body>
</html>