-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheets.js
More file actions
83 lines (77 loc) · 2.46 KB
/
sheets.js
File metadata and controls
83 lines (77 loc) · 2.46 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
// take a count of digits and turn it into the top number for use in a random range
function getMax(digits) {
digits = parseInt(digits) || 2;
return parseInt("9".repeat(digits));
}
// get a random integer between 0 and a maximum number
function getRandomInt(max) {
return Math.round(Math.random() * (max + 1));
}
function getSorted(a, b) {
return a - b;
}
// produce an array of math problems with a top number, a bottom number, an operator, an answer
function makeProblems(digits, operator) {
var p = [];
for (var i=0; i<25; i+=1) {
// get random numbers with the biggest one first
// TODO: figure out "negative numbers" feature here and in controls
var numbers = [];
numbers.push(getRandomInt(getMax(digits)));
numbers.push(getRandomInt(getMax(digits)));
numbers = numbers.sort(getSorted).reverse();
// figure out the answer for each problem
var topNum = numbers[0];
var bottomNum = numbers[1];
var answer = 0;
switch(operator) {
case "+":
answer = topNum + bottomNum;
break;
case "-":
answer = topNum - bottomNum;
break;
case "x":
answer = topNum * bottomNum;
break;
case "÷":
topNum = topNum || 1;
bottomNum = bottomNum || 1;
var remainder = topNum % bottomNum;
answer = Math.floor(topNum / bottomNum) + (remainder ? " r" + remainder : "");
break;
}
p.push({topNum: topNum, bottomNum: bottomNum, operator: operator, answer: answer})
}
return p;
}
Vue.component('problem-item', {
props: ['problem', 'showAnswers']
});
var app = new Vue({
el: '#problem-container',
data: {
digits: 2,
operator: "+",
showAnswers: false
},
computed: {
problems: function() {
return makeProblems(this.digits, this.operator);
}
},
methods: {
print: function(event) {
var controls = "digits="+this.digits+",operator="+this.operator+",showAnswers="+this.showAnswers;
try {
gtag('event', 'print', {
'event_category' : 'Controls',
'event_label' : controls
});
window.print();
} catch(e) {
console.error(e);
}
}
}
});