JavaScript automatically generate equation for kidssteemCreated with Sketch.

in #development9 years ago (edited)

chrome_2017-10-27_21-50-29.png

My daughter is learning the arithmetic, I acquiesced in that she can use the calculator, but after 3 times testing, her manually calculating ability always lower the average level. A result is her math teacher talked with me for this issue. So I have to ask my daughter to exercise manually, but we have no existing equation, to be a programmer, I must not to design equation by myself. :-)

So I wrote some javascript code to generate equation, maybe it can help you guys for the same situation. The code is below, just copy&paste them into a HTML file, open in browse, pick the "generate equation" button, have fun!

<html>
<body>

<script type="text/javascript">
function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function compute()
{
    var signs=new Array();
    signs[0]=" + ";
    signs[1]=" - ";
    signs[2]=" x ";
    signs[3]=" ÷ ";

    document.getElementById("p1").innerHTML=Math.random().toFixed(2)+ signs[getRandomInt(0,3)] +(100*Math.random()).toFixed(2)+ signs[getRandomInt(0,3)] +(10*Math.random()).toFixed(2);
}
</script>
<input type="button" value="Generate equation" style="font-size:60px;height:200px;width:600px" onclick=compute()>
<p style="font-size:80px;" id="p1"></p>
</body>
</html>

An issue is sometimes it will generate the negative equation likes below, if anyone have a good idea for this, please leave a message.

chrome_2017-10-27_21-50-52.png

Coin Marketplace

STEEM 0.05
TRX 0.33
JST 0.079
BTC 63481.02
ETH 1684.55
USDT 1.00
SBD 0.42