Results 1 to 4 of 4

Thread: Math Tutoring Page

  1. #1
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);
    	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

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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;
    	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.
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

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

    Default

    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.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

Bookmarks

Posting Permissions

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