1. New Comer (less than 5 posts) Join Date
Apr 2009
Posts
1
Thanks
0
Thanked 0 Times in 0 Posts

## Math Tutoring Page

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: 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:
Code:
```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);

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  2. ## Well, there are a few things you can try.
You can do this, which adds a to b, guaranteeing that it's higher:
Code:
```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;

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. 3. ## My JS skills suck, but couldn't you also do something like:

Code:
`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. 4. Senior Coders Join Date
Jun 2007
Posts
543
Thanks
3
Thanked 78 Times in 78 Posts
Blog Entries
1

## To find a random number between to numbers: `Math.ceil(Math.random()*({max}-{min}))+{min}`, so in your situation:
Code:
```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. #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•