-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMath Generator.html
557 lines (548 loc) · 26.1 KB
/
Math Generator.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<html>
<head>
<title>Math Problem Generator 0.6.7</title> <!-- closed beta, not to be used without developer permission -->
<script>
//this copy has implementation of output mode two.
var currentWindow = window; //no longer neccesary but need to check for uses
var spaces = 5; //line breaks between problems, is used in ' "<br/>".repeat(spaces) ' to insert that many line breaks, unsure how to use in second omode.
var probs = 10;// how many problems to generate
var probsPerLine = 10;
var sameDen = false; //fraction prblems use same denminator inputs
var simpAft = true; //simplify answer after fraction problem
var Omode = 0; //output mode two is the mode where problems appere in classic worksheet format with a liine underneath and a symbol on the side
var PNM = 0; //Problem Number Mode, the way of outputing the problem number 1 is 'i+". "'. 2 is '"("+i+") "'
var numDirec = 0; //Direction of numbering for problems. 0 is horizontal and 1 is verticle.
function rNum(i){// attempt to number problems horizontaly with more than one row of questions.
var rows = Math.ceil(probs/probsPerLine);//currently does not work
var x = i % rows;
var y = i % probsPerLine; //starts at top of page
if(numDirec==0){
var newI = x + y*rows;
}
if(numDirec==1){
var newI = x*probsPerLine + y + function(){if(y==0);return 1;};
}
return newI;
}
function out(i){
i = rNum(i);//creates numbering system, either .1 xxx or (1) xxx
if(PNM==0){//plan to implement lettering for problems
return i + ". ";
}
if(PNM==1){
return "(" + i + ") ";
}
}
function random(digits){ //creates a random number with as many digits as requested, does not return one to prevent overeasy problems.
if(digits!=0){
var x = Math.floor(Math.random(1) * ("9".repeat(digits)))+1;
if(getLen(x) == digits){
if(x == 1){
return random(digits); //if output is one call function again, not best way but works pretty well.
}
else{
return x;
}
}
else{
return random(digits); //if output is wrong number of digits call this function again.
}
}
else{
return 0;
}
}
function getLen(number) { //simple function to get the length in digits of a number.
return number.toString().length;
}
var countDecimals = function (value) { //same as above but counts decimal digits, got this on code stackoverflow
if ((value % 1) != 0)
return value.toString().split(".")[1].length;
return 0;
};
function reduce(numerator,denominator){ //for reducing fractions, I got this code online and I have no idea how it works.
var gcd = function gcd(a,b){
return b ? gcd(b, a%b) : a;
};
gcd = gcd(numerator,denominator);
return [numerator/gcd, denominator/gcd];
}
function division(decimals){
for(var i = 1; i<=probs; i++){
var r1 = 3;
var r2 = 2;
var d = 0;
if(decimals==1){r1=2;r2=1;d=0;}
if(decimals==2){r1=3;r2=1;d=0;}
if(decimals==3){r1=4;r2=1;d=0;}
if(decimals==4){r1=3;r2=2;d=0;}
if(decimals==5){r1=4;r2=2;d=0;}
var n2 = random(r2);
var n1 = random(r1);
var a = n1 / n2;
if(countDecimals(a) == d){
changeOutputCol(i)
if (Omode==0){//Uses "|" and overline to make division shape
document.getElementById("output").innerHTML += out(i) + "<br/>    " + n2 + "|<span style='text-decoration: overline'>" + n1.toLocaleString() + "</span>" + "<br/>".repeat(spaces-1);
}
if (Omode==1){
document.getElementById("output").innerHTML += out(i) + n1.toLocaleString() + ' ÷ ' + n2 + " = " + "<br/>".repeat(spaces);
}
document.getElementById("output2").innerHTML += out(i) + a + "<br/>";
}
else{
i = i - 1;
}
}
}
function addFractions(){
for(var i = 1; i<=probs; i++){
var n1 = random(1);
var n2 = random(1);
var n3 = random(1);
if (sameDen == true){var n4 = n2;}
else {var n4 = random(1);}
while(n1 == n2){
var n1 = random(1);
var n2 = random(1);
if (sameDen == true){var n4 = n2;}
}
while(n3 == n4){
var n3 = random(1);
var n4 = random(1);
if (sameDen == true){var n4 = n2;}
}
var m1 = n1 * n4;
var m2 = n2 * n4;
var m3 = n3 * n2;
var m4 = n4 * n2;
var f1 = m1 + m3;
var f2 = m2;
var a = reduce(f1,f2);
var a2 = a[0] + "/" + a[1];
changeOutputCol(i)
document.getElementById("output").innerHTML += out(i) + n1 + "/" + n2 + " + " + n3 + "/" + n4 + " = " + "<br/>".repeat(spaces);
document.getElementById("output2").innerHTML += out(i) + a2 + "<br/>";
}
}
function subFractions(){
for(var i = 1; i<=probs; i++){
var n1 = random(1);
var n2 = random(1);
var n3 = random(1);
var n4 = random(1);
if (sameDen == true){var n4 = n2;}
while(n1 == n2){
var n1 = random(1);
var n2 = random(1);
if (sameDen == true){var n4 = n2;}
}
while(n3 == n4){
var n3 = random(1);
var n4 = random(1);
if (sameDen == true){var n4 = n2;}
}
var m1 = n1 * n4;
var m2 = n2 * n4;
var m3 = n3 * n2;
var m4 = n4 * n2;
var f1 = m1 - m3;
var f2 = m2;
var a = reduce(f1,f2);
var a2 = a[0] + "/" + a[1];
if(a[0]/a[1] > 0){
changeOutputCol(i)
document.getElementById("output").innerHTML += out(i) + n1 + "/" + n2 + " - " + n3 + "/" + n4 + " = " + "<br/>".repeat(spaces);
document.getElementById("output2").innerHTML += out(i) + a2 + "<br/>";
}
else{
i = i-1;
}
}
}
function simpFractions(){
for(var i = 1; i<=probs; i++){
var n1 = random(1);
var n2 = random(1);
var n1 = n1*random(1);
var n2 = n2*random(1);
var a = reduce(n1,n2);
var a2 = a[0] + "/" + a[1];
if(a[0]/a[1] > 0 && a[1] != n2){
changeOutputCol(i)
document.getElementById("output").innerHTML += out(i) + n1 + "/" + n2 + " = " + "<br/>".repeat(spaces);
document.getElementById("output2").innerHTML += "<br/>" + out(i) + a2;
}
else{
i = i-1;
}
}
}
function multFractions(){
for(var i = 1; i<=probs; i++){
var n1 = random(1);
var n2 = random(1);
var n3 = random(1);
var n4 = random(1);
if (sameDen == true){var n4 = n2;}
var m1 = n1 * n3;
var m2 = n2 * n4;
if(simpAft == true){var a = reduce(m1,m2);}
else{var a = [m1,m2];}
if(a[0]/a[1] > 0){
changeOutputCol(i)
document.getElementById("output").innerHTML += out(i) + n1 + "/" + n2 + " × " + n3 + "/" + n4 + " = " + "<br/>".repeat(spaces);
document.getElementById("output2").innerHTML += out(i) + a[0] + "/" + a[1] + "<br/>";
}
else{
i = i-1;
}
}
}
function multiplication(digits){
for(var i = 1; i<=probs; i++){
var n1 = random(Math.ceil(digits/2));
var n2 = random(Math.floor(digits/2));
var a = n1 * n2;
changeOutputCol(i)
if (Omode==0){
document.getElementById("output").innerHTML += "<div>" + out(i) + n1.toLocaleString() + ' × ' + n2.toLocaleString() + "</div>" + "<br/>".repeat(spaces);
}
if (Omode==1){
document.getElementById("output").innerHTML += out(i) + n1.toLocaleString() + ' × ' + n2.toLocaleString() + " = " + "<br/>".repeat(spaces);
}
document.getElementById("output2").innerHTML += out(i) + a.toLocaleString() + "<br/>";
}
}
function Square(digits){
for(var i = 1; i<=probs; i++){
var n = random(digits);
var a = n * n;
changeOutputCol(i)
document.getElementById("output").innerHTML += out(i) + n.toLocaleString() + " = " + "<br/>".repeat(spaces);
document.getElementById("output2").innerHTML += out(i) + a.toLocaleString() + "<br/>";
}
}
function SQRT(digits){
for(var i = 1; i<=probs; i++){
var a = random(digits);
var n = a * a;
changeOutputCol(i)
if (Omode==0){
document.getElementById("output").innerHTML += out(i) + ' ✓<span style="text-decoration: overline">' + n.toLocaleString() + "</span> = " + "<br/>".repeat(spaces);
}
if (Omode==1){
document.getElementById("output").innerHTML += out(i) + ' ✓ ' + n.toLocaleString() + " = " + "<br/>".repeat(spaces);
}
document.getElementById("output2").innerHTML += out(i) + a.toLocaleString() + "<br/>";
}
}
function createProblems(inp){
clearCols()
document.getElementById("output").innerHTML = "Questions<br/>";
document.getElementById("output2").innerHTML = "Answers<br/>";
if(document.getElementById("option").value == "Division"){
division(document.getElementById("digits").value);
}
if(document.getElementById("option").value == "Multiplication"){
multiplication(document.getElementById("digits").value);
var digits = document.getElementById("digits").value;
}
if (document.getElementById("option").value == "Frac"){
if(document.getElementById("optionFrac").value == "AddFractions"){
addFractions();
}
if(document.getElementById("optionFrac").value == "SubFractions"){
subFractions();
}
if(document.getElementById("optionFrac").value == "SimpFractions"){
simpFractions();
}
if(document.getElementById("optionFrac").value == "MFRAC"){
multFractions()
}
}
if(document.getElementById("option").value == "SQRT"){
SQRT(document.getElementById("digits").value);
}
if(document.getElementById("option").value == "SQR"){
Square(document.getElementById("digits").value);
}
}
var answers = "";
function showAnswers(){
document.getElementById("output2").style.visibility = "visible";
document.getElementById("output2").classList = "";
document.getElementById("output2").style.width = "100px";
document.getElementById("HSButton").value = "Hide Answers";
document.getElementById("HSButton").onclick = hideAnswers;
}
function hideAnswers(){
document.getElementById("output2").style.visibility = "hidden";
document.getElementById("output2").style.width = "0px";
document.getElementById("output2").classList = "noPrint";
document.getElementById("HSButton").value = "Show Answers";
document.getElementById("HSButton").onclick = showAnswers;
}
function undoSameDen(){
document.getElementById("sameDenChanger").innerText = "False";
document.getElementById("sameDenChanger").onclick = doSameDen;
sameDen = false;
}
function doSameDen(){
document.getElementById("sameDenChanger").innerText = "True";
document.getElementById("sameDenChanger").onclick = undoSameDen;
sameDen = true;
}
function undoSimpAft(){
document.getElementById("simpAftChanger").innerText = "False";
document.getElementById("simpAftChanger").onclick = doSimpAft;
simpAft = false;
}
function doSimpAft(){
document.getElementById("simpAftChanger").innerText = "True";
document.getElementById("simpAftChanger").onclick = undoSimpAft;
simpAft = true;
}
function showAdvance(){
document.getElementById("overlay").style.display = "block";
document.getElementById("menu").style.display = "block";
}
function hideAdvance(){
document.getElementById("overlay").style.display = "none";
document.getElementById("menu").style.display = "none";
}
function changeFontSize(size){
if (size=="s"){
document.getElementById("outputs").style.fontSize = "100%";
}
if (size=="m"){
document.getElementById("outputs").style.fontSize = "150%";
}
if (size=="l"){
document.getElementById("outputs").style.fontSize = "200%";
}
}
function changeOutputCol(i){
if((i-1) % probsPerLine == 0 && i != 1){
document.getElementById("output").id = "outputx";
document.getElementById("outputs").innerHTML += '<div id="output" class="output"><br/></div>';
}
}
function clearCols(){
document.getElementById("outputs").innerHTML = '<div id="output" class="output">Questions <br/></div><div id="output2">Answers <br/></div>';
}
function changeMode(){
var changed = false;
document.getElementById("inputEasyLabel").style.visibility = "visible";
document.getElementById("digits").style.visibility = "visible";
if (document.getElementById("option").value == "Division"){
document.getElementById("inputEasyLabel").innerText = "Difficulty (1-5):";
changed = true;
}
if (document.getElementById("option").value == "Multiplication"){
document.getElementById("inputEasyLabel").innerText = "Problem digits:";
changed = true;
}
if (document.getElementById("option").value == "SQRT"){
document.getElementById("inputEasyLabel").innerText = "Digits of answer:";
changed = true;
}
if (document.getElementById("option").value == "SQR"){
document.getElementById("inputEasyLabel").innerText = "Digits of problem:";
changed = true;
}
if (changed == false){
document.getElementById("inputEasyLabel").style.visibility = "hidden";
document.getElementById("digits").style.visibility = "hidden";
}
}
function onLoad(){
divisionMode();
}
function isNumber(inp){
var val = parseInt(inp.value);
var min = inp.min;
var max = inp.max;
if(inp.value != ""){
if (Number.isInteger(val) == false){
inp.value = min;
}
if (val > max){
inp.value = max;
}
if (val < min){
inp.value = min;
}
inp.value = Math.round(inp.value);
}
else{
inp.value = "";
}
}
function fracView(){
if(document.getElementById("option").value == "Frac"){
document.getElementById("optionFrac").style.visibility = "visible";
document.getElementById("optionFrac").style.width = "auto";
}
else{
document.getElementById("optionFrac").style.visibility = "hidden";
document.getElementById("optionFrac").style.width = "0";
}
}
</script>
<style>
#answer2{
visibility: hidden;
}
#optionFrac{
visibility: hidden;
width: 0;
}
#output{
width: 120px;
float: left;
}
#outputx{
width: 120px;
float:left;
}
#output2{
float:right;
font-size: 100%;
width: 100px;
}
@media print{
#output{
float: left;
}
#output2{
float:right;
}
.noPrint{
visibility: hidden;
}
}
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.5);
z-index: 2;
cursor: pointer
}
#menu {
position: fixed;
display: none;
width: 50%;
height: 50%;
top: 25%;
left: 25%;
right: 25%;
bottom: 25%;
background-color: rgba(255, 255, 255, 1);
z-index: 10;
cursor: pointer;
}
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
padding: 5px 0;
border-radius: 6px;
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
</head>
<body>
<select id="option" class="noPrint" onchange="changeMode();fracView();">
<option value="Division">Division</option>
<option value="Multiplication">Multiplication</option>
<option value="SQRT">Square Root</option>
<option value="SQR">Squares</option>
<option value="Frac">Fraction</option>
</select>
<select id="optionFrac" class="noPrint">
<option value="AddFractions">Add Fractions</option>
<option value="SubFractions">Subtract Fractions</option>
<option value="SimpFractions">Simplify Fractions</option>
<option value="MFRAC">Multiply Fractions</option>
<option value="MFRAC">Divide Fractions</option>
</select>
<input type="button" onclick="createProblems(window);" value="Go" class="noPrint"/>
<span class="noPrint" id="inputEasyLabel">Difficulty (1-5):</span><input type="number" id="digits" onchange="isNumber(this);" class="noPrint" max="5" min="1" value="1"/></br>
<input type="button" onclick="window.print();" value="Print" class="noPrint"/>
<input type="button" onclick="hideAnswers();" id="HSButton" value="Hide Answers" class="noPrint"/>
<input type="button" onclick="showAdvance();" id="SAButton" value="Show Advanced Menu" class="noPrint"/>
<div id="outputs">
<div id="output" class="output">
Questions
</div>
<div id="output2">
Answers
</div>
</div>
<div id="overlay"></div>
<div id="menu">
<div style="height: 20%; width: 35%; float: left;">
<table style="width:100%">
<tr>
<td>Font size:</td>
<td>
<select onchange="changeFontSize(this.value);" style="width: 75%;">
<option value="s" style="font-size:100%">Small</option>
<option value="m" style="font-size:150%">Medium</option>
<option value="l" style="font-size:200%">Large</option>
</select>
</td>
</tr>
<tr>
<td>Line Spaces:</td>
<td><input style="width: 75%;" onchange="isNumber(this);" onclick="spaces = this.value;" type="number" class="noPrint" max="5" min="1" value="4" disabled/></td>
</tr>
<tr>
<td>Number of problems:</td>
<td><input style="width: 75%;" onchange="isNumber(this);probs = this.value;" type="number" class="noPrint" max="50" min="5" value="10" disabled/></td>
</tr>
<tr>
<td>Problems per line:</td>
<td><input style="width: 75%;" onchange="isNumber(this);probsPerLine = this.value;" type="number" class="noPrint" max="20" min="1" value="10" disabled/></td>
</tr>
<tr>
<td>Numbering mode:</td>
<td>
<select onchange="PNM = this.value;" style="width: 75%;">
<option value=0>1. </option>
<option value=1>(1)</option>
<option value=0 disabled>a. </option>
<option value=1 disabled>(a)</option>
</select>
</td>
</tr>
</table>
</div>
<br/><div style="float: left; width: 50%;">
Fractions:<br/><br/>
<text title="Use the same denominator for fraction problems; means students do not have to convert fractions to same form.">Use same denominator: </text><text onclick="doSameDen();" id="sameDenChanger">False</text><br/>
<text title="After doing fraction problems simplify answer; means students have to simplyfy their answers after solving the problem.">Simplify answer for fraction problems: </text><text onclick="doSimpAft();" id="simpAftChanger">False</text>
</div><br/>
<input type="button" onclick="hideAdvance();" id="SAButton" value="Done" class="noPrint"/>
</div>
</body>
</html>