...

# Creating math sheet generator

JamesSmith75
12-09-2009, 11:43 AM
I have been asked by a friend to create a math sheet generator for their child, however i don't have a clue where to start with javascript.

Similar to this..
http://cemc2.math.uwaterloo.ca/mathfrog/english/teacher/sheetgen.shtml

It doesn't need to be advanced as that as all i'm looking for is how to create the sums.

If someone could help either by informing me where to start or even an online tutorial i would be extremely greatful!

Many thanks,
James

jmrker
12-09-2009, 05:20 PM
You can play with this to create different difficulty problem sets.
Area to vary is the 'xvals' and 'yvals' array set-up. Lots of freedom to change this to your liking.

Remove the drop down options list if you only want addition problems.
If you come up with additional improvement beyond simplistic display, post back here for other to see. :thumbsup:

<html>
<title>Math WorkSheet</title>
<script type="text/javascript">

var NProblems = 25;
var xvals = [];
var yvals = [];
for (var i=0; i<NProblems; i++) {
// xvals.push(i); yvals.push(i+1); // limit to problem values to (0,1) ... 25
xvals.push(i % 10); yvals.push((i % 10)+1); // limit to single digit problems
}

function MakeTable(act) {
var tmp = '';
var str = '<table border="1" width="80%"><tr>';
i = 0;
while (i < NProblems) {
x = xvals[i];
y = yvals[i];
str += '<td align="right">'+x;
if (act == 'add') { str += '<br>+ '+y; tmp = x+y; }
if (act == 'sub') { str += '<br>- '+y; tmp = x-y; }
if (act == 'mul') { str += '<br>* '+y; tmp = x*y; }
if (act == 'div') { str += '<br>/ '+y; tmp = (x/y).toFixed(2); }
str += '<br>_____';
if (document.getElementById('answers').checked) { str += '<br>'+tmp; }
else { str += '<br>&nbsp;' }
str += '<br>&nbsp;</td>';
i++;
if ((i % 5) == 0) { str += '</tr><tr>'; }
}
str += '</tr></table>';
return str;
}

function GenerateWS() {
var x = 0; var y = 0; var str = '';
var str = '';
var sel = document.getElementById('MathAction').value;
switch (sel) {
case 'add' : str += MakeTable(sel); break;
case 'sub' : str += MakeTable(sel); break;
case 'mul' : str += MakeTable(sel); break;
case 'div' : str += MakeTable(sel); break;
default : alert('No choice selected'); break;
}
document.getElementById('TBL').innerHTML = str;
}
function randOrd() {
return (Math.round(Math.random())-0.5);
}
function NewSet() {
xvals.sort(randOrd);
yvals.sort(randOrd);
}

</script>
<body>
<select id="MathAction">
<option value="sub">Subtraction</option>
<option value="mul">Multiplication</option>
<option value="div">Division</option>
</select>
<button onclick="NewSet()">New worksheet</button>
<button onclick="GenerateWS()">Generate Worksheet</button>
<div id="TBL"></div>
</body>
</html>

Good Luck!
:)

JamesSmith75
12-11-2009, 02:05 PM
Thats bang on the money mate thank you very much! :thumbsup: Would it be possible to generate the actual worksheet on a different page or would that be more hassle than it's worth?

Thanks again! :)

jmrker
12-11-2009, 04:04 PM
Thats bang on the money mate thank you very much! :thumbsup: Would it be possible to generate the actual worksheet on a different page or would that be more hassle than it's worth?

Thanks again! :)

Sure ... no problem with display.

Save the program with a different name with this one line change:

<button onclick="NewSet()">New worksheet</button>
<button onclick="GenerateWS()">Generate Worksheet</button>

Minor problem with generation in that because of the random effect,
you might not get the same worksheet witht the same answers.
Would take a little more coding to save answers for a particular worksheet somewhere.

Anyway, you're most welcome. Happy to help!
Good Luck and have fun with it.
:)