-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice.htm
More file actions
378 lines (351 loc) · 9.41 KB
/
Copy pathPractice.htm
File metadata and controls
378 lines (351 loc) · 9.41 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
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
<!DOCTYPE html>
<html>
<!-- **************************************************************************
Math Practice Generator
Creator: Michael Anthony Abril (GitHub: coolfizzin)
License: GPL v.3.0
Updated: 12/22/2018
Originally inspired by code from http://leancrew.com/all-this/2008/01/math-practice-sheet-in-html-and-javascript/
******************************************************************************* -->
<head>
<title>Math Practice : CatholicFamily.Fun</title>
<style type="text/css">
@page {
size: letter portrait;
margin: 0.5in 0.75in 0 0.75in;
}
body {
width:7in;
}
h1 {
text-align: center;
font-family: Sans-Serif;
font-weight: bold;
font-size: 32px;
margin: 0;
padding: 0;
}
main {
width:100%;
text-align: center;
color: black;
background-color: white;
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-items: center;
align-content: space-around;
}
div {
flex-shrink:0.5;
flex-basis: 20%; /* 16.6% Adjust for fitting more in a row */
padding: 37px 0;
}
div.size-6x6 {
flex-shrink:0.3;
flex-basis: 16.6%;
}
table {
font-family: Times New Roman;
font-size: 30pt;
text-align: right;
border-spacing:0;
margin: auto;
}
div.size-6x6 table {
font-size: 24pt;
}
td {
//border:solid black 1px;
padding:0 4px;
vertical-align:center;
overflow:none;
line-height:1.05em;
}
.sign {
text-align:left;
width:0.5em;
line-height:1em;
vertical-align:middle;
font-size:0.8em;
}
.num {
min-width:1em
}
.line {
border-bottom:0.2rem solid #000;
padding-bottom:7px;
}
</style>
<script language="Javascript">
/* //////////////////
CLOCKWORKS
////////////////// */
var count = 25; // default: 5x5
var size = "5x5";
var sign, action, min, max, by, bymax, urlParams, variations, onerange, tworange;
/* ========================
HELPER FUNCTIONS
======================== */
function factorial(num) {
if (num < 0)
return -1;
else if (num == 0)
return 1;
else
return (num * factorial(num - 1));
}
function sumto(num) {
if (num < 1)
return -1;
else if (num == 1)
return 1;
else
return (num + sumto(num - 1));
}
function combo(r, n) {
return factorial(r+n-1)/(factorial(r)*factorial(n-1));
}
function getOverlap(a,b,c,d) {
var low = Math.max(a,c);
var high= Math.min(b,d);
var range=1 + high - low;
return Math.max(range, 0); // Don't return less than 0
}
//alert(combo(2,5));
//To get combination: (r+n-1)!/(r!(n-1)!)
/* =======================
PROCESS INPUT
======================= */
(window.onpopstate = function () {
var match,
pl = /\+/g, // Regex for replacing addition symbol with a space
search = /([^&=]+)=?([^&]*)/g,
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
query = window.location.search.substring(1);
urlParams = {};
while (match = search.exec(query))
urlParams[decode(match[1])] = decode(match[2]);
})();
sign = 'plusminus';
action = 'Addition & Subtraction';
if ('size' in urlParams && urlParams['size']=="6x6") {
count = 36;
size = "6x6";
}
if ('sign' in urlParams && urlParams['sign']!='') {
switch (urlParams['sign']) {
case ('addition'):
sign = '+';
var action = 'Addition';
break;
case ('subtraction'):
sign = '−';
var action = 'Subtraction';
break;
case ('multiplication'):
sign = '×';
var action = 'Multiplication';
break;
case ('division'):
sign = '÷';
var action = 'Division';
break;
case ('plusminus'):
sign = 'plusminus';
var action = 'Addition & Subtraction';
break;
case ('multidivide'):
sign = 'multidivide';
var action = 'Multiplication & Division';
break;
case ('all'):
sign = 'all';
var action = 'Comprehensive Review';
break;
}
}
max = ('max' in urlParams && urlParams['max']!='') ? parseInt(urlParams['max']) : 12;
min = ('min' in urlParams && urlParams['min']!='') ? parseInt(urlParams['min']) : 0;
if (max < min)
max = min;
if (urlParams['second']=='specific') {
if ('by' in urlParams && urlParams['by']!='')
by = parseInt(urlParams['by']);
else if (max > min && max > 0)
by = Math.floor((max - min)/2); // Try to guess a specific by
else
by = min;
bymax = by;
}
else if (urlParams['second']=='range') {
by = ('by' in urlParams && urlParams['by']!='') ? parseInt(urlParams['by']) : min;
bymax = ('bymax' in urlParams && urlParams['bymax']!='') ? parseInt(urlParams['bymax']) : max;
// Validation
if (bymax < by)
bymax = by;
}
else {
by = min;
bymax = max;
}
/* ========================
CALCULATE VARIATIONS
======================== */
onerange = (1 + max - min);
tworange = (1 + bymax - by);
//### Method 1: Get overlap, then adjust for non-overlap
/*
// Non-overlapping ranges
if (by > max || bymax < min) {
variations = onerange * tworange;
}
// Overlapping ranges
else {
variations = combo(2, onerange);
//var debug = variations;
//alert(variations + '-' + sumto(by - min) + '+' + onerange * (bymax - max));
if (by > min) {
variations -= sumto(by - min);
//debug += '-' + sumto(by - min);
}
else if (by < min) {
variations += (onerange * (min - by));
//debug += '+' + onerange * (min - by);
}
if (bymax > max) {
variations += (onerange * (bymax - max));
//debug += '+' + onerange * (bymax - max);
}
else if (bymax < max){
variations -= sumto(max - bymax);
//debug += '-' + sumto(max - bymax);
}
//alert(debug);
}
*/
//### Method 2: Simple formula (Accurate)
// First get total possibilities (A * B)
variations = (onerange * tworange);
// Then subtract for overlap
variations-=sumto(getOverlap(min, max, by, bymax)-1);
//### Finish variations
//alert(variations);
var did ={ // For remembering already-used problems
"+" : new Array(),
"−" : new Array(),
"×" : new Array(),
"÷" : new Array()
};
var posted=new Array(); // For recalling what is above or below
/* ========================
GENERATE PROBLEM
======================== */
function single_problem(idx) {
var iterations = 0;
do {
iterations ++;
var operator, top, bottom, key;
if (sign=='plusminus') {
operator = "+";
if (Math.random() < 0.5)
operator = "−";
}
else if (sign=='multidivide') {
operator = "×";
if (Math.random() < 0.5)
operator = "÷";
}
else if (sign=='all') {
operator = "+";
if (Math.random() < 0.25)
operator = "−";
else if (Math.random() < 0.5)
operator = "×";
else if (Math.random() < 0.75)
operator = "÷";
}
else {
operator = sign;
}
// a = Second number (which can be set by the form)
var a, b, c;
if (urlParams['second']=='specific')
a = by;
else if (urlParams['second']=='range')
a = Math.floor(Math.random()*((bymax + 1)-by) + by);
else
a = Math.floor(Math.random()*((max + 1)-min) + min);
// The answer to addition, or first number for subtraction, which will always be either the same as a or up to 12 higher
if (operator == "+" || operator == "−") {
c = Math.floor(Math.random()*((a+max+1)-(a+min)) + (a+min));
b = c - a;
}
// The answer to multiplication, or first number for division, which will always be either the same as a or up to 12 higher
else if (operator == "×" || operator == "÷") {
b = Math.floor(Math.random()*((max + 1)-min) + min);
c = a * b;
}
if (operator == "+" || operator == "×") {
top = b;
bottom = a;
if (b <= a) // Always put lower number first so both ways are counted as the same
key = b + operator + a;
else
key = a + operator + b;
}
else {
top = c;
bottom = a;
if (c <= a)
key = c + operator + a;
else
key = a + operator + c;
}
//# Check for redundancy
if (iterations < 50) { // failsafe to avoid excessive looping
// Is this one directly to the left or above?
if ((idx>0 && posted[idx-1]==key) || (idx>=5 && posted[idx-5]==key))
continue;
// Have all options been used before repeating?
if (did[operator].includes(key)) // iterations is a
continue;
}
//# Success
iterations = 0;
did[operator].push(key);
posted[idx]=key;
// Reset variations counting if full
if (did[operator].length >= variations)
did[operator] = new Array();
// Finally write it
return '<div class="size-' + size + '"><table>' +
'<tr><td/><td class="num">' + top + '</td></tr>' +
'<tr><td class="sign line">' + operator + '</td><td class="line">' + bottom + '</td></tr>' +
'</table></div>';
} while (1) // Repeats only if attempt fails
}
</script>
</head>
<body>
<h1 id='title'>Math Practice</h1>
<main>
<script language="Javascript">
/* //////////////////
PROCEDURE
////////////////// */
for (i=0; i<count; i++){
document.write(single_problem(i));
}
// Set title
var title=document.getElementById('title');
if (urlParams['second']=='specific' && by != -1)
title.innerHTML = action + ': ' + by;
else if (urlParams['second']=='range' && by != -1 && bymax != -1)
title.innerHTML = action + ': ' + by + ' to ' + bymax;
else
title.innerHTML = action;
</script>
</main>
</body>
</html>