PDA

View Full Version : Math Tutoring Page



benjamin182
04-21-2009, 08:53 PM
So I have a very simple math tutoring page I'm working on for a final project in college. The project is done concerning the problem statement, but there is one part that I'd like to "take the extra mile".
Here is what the page looks like:

http://bennyboy.us/af.jpg
Basically, there is a button that generates a new number in two text boxes. The user is asked to find the difference between those two random numbers, put his answer in the answer box. When ready, the user clicks the "check answer" button.
That is all done. The only thing I would like to further add to this tool is that when the user clicks the "New Problem" button, I want to be sure that the first number is always larger than the second number ( so that the user never has to give a negative answer).
So basically, I just need to know how to program the "New Problem" button for the subtraction page. Here is what I have now:

function newProbSub()
{
var a, b;

a = document.getElementById('randNum1').value = Math.floor(Math.random()*25);
b = document.getElementById('randNum2').value = Math.floor(Math.random()*25);
document.getElementById('answer').value = 0;

if (a<b)
{
document.getElementById('randNum1').value = Math.floor(Math.random()*25);
}

else
{
}
}

Its not working... Here is a link to the page itself: http://bennyboy.us/cs/math/sub.html I just need to make sure that each time the user clicks "New Problem", that random number "a" is always larger than random number "b". Does someone have advice on how to accomplish my task? Thank you :D

Nile
04-21-2009, 11:27 PM
Well, there are a few things you can try.
You can do this, which adds a to b, guaranteeing that it's higher:


function newProbSub()
{
var a, b;

a = document.getElementById('randNum1').value = Math.floor(Math.random()*25);
b = document.getElementById('randNum2').value = Math.floor(Math.random()*25)+a;
document.getElementById('answer').value = 0;

if (a<b)
{
document.getElementById('randNum1').value = Math.floor(Math.random()*25);
}

else
{
}
}

Or you could subtract b and a, but try the code above.

Medyman
04-21-2009, 11:34 PM
My JS skills suck, but couldn't you also do something like:


Math.floor((25-(a-1))*Math.random()) + a

So that would give you a random number between a and 25. Of course, if a is 24 or 25, that would confuse things.

Master_script_maker
04-21-2009, 11:46 PM
To find a random number between to numbers: Math.ceil(Math.random()*({max}-{min}))+{min}, so in your situation:
var a=Math.ceil(Math.random()*(25));
var b=Math.ceil(Math.random()*(25-a))+a;
There is a problem with this though, because it will sometimes return the max value.