-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathtest.js
More file actions
76 lines (69 loc) · 1.82 KB
/
Copy pathmathtest.js
File metadata and controls
76 lines (69 loc) · 1.82 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
/*
MathTest:
.check() to validate an answer and move to next test if good
.onUpdate() to set a callback for display of results and such
*/
var MathTest = function() {
var updater, tests, curTest, failed, passed, encouragements;
// return list of math pairs, 1-12 each
function scrambledPairs() {
var r = [], a, b;
for (a = 1; a <= 12; a += 1) {
for (b = 1; b <= 12; b += 1) {
r.push([a, b]);
}
}
return _.shuffle(r);
}
function newtest() {
tests = scrambledPairs();
curTest = 0;
encouragements = parseInt(Math.random() * 4) + 4;
}
function update(fail, msg) {
updater(tests[curTest][0],
tests[curTest][1],
fail,
msg);
}
failed = passed = 0;
newtest();
return {
onUpdate: function(cb) {
updater = cb;
update();
},
check: function(a, b, r) {
var fail, msg;
if (-1 === r.search('[0-9]+')) {
// no answer to check
return;
}
if (a * b == r) {
passed++;
curTest++;
if (curTest === tests.length) {
// completed another full set of tests
var i = completed = passed / tests.length;
msg = '';
for (; i > 0; i--) {
msg += '* ';
}
msg += 'passed ' + completed + ' complete test';
msg += (completed > 1 ? 's' : '');
newtest();
} else if (0 === curTest % parseInt(tests.length / encouragements)) {
// give some encouragements along the way
var percentComplete = parseInt(100 * curTest / tests.length);
msg = 'great work! ' + percentComplete + '% done';
}
update();
} else {
console.log('failed ' + tests[curTest]);
failed++;
fail = true;
}
update(fail, msg);
}
};
}();