dharvell
05-08-2008, 02:43 PM
I'm quite the n00b when it comes to javascripting, so this may be rather elementary... apologies in advance! :) I am building a "math facts" application for my young children. These apps cover addition, subtraction, multiplication, and division. Currently, I am working on the division app.
Basically, I have three variables: insideNumber, outsideNumber, and answer. To be sure my division problems will make sense to a 3rd grader, I have some basic rules these variables need to follow. To make the math problems follow these rules, I need some sort of logic to discard the numbers assigned to these variables, until the problem is appropriate for my 3rd grader. Once the problem is appropriate, I need the numbers to be displayed on the screen. I may be going about it all wrong, but so far, what I have is as follows:
var insideNumber = 0; // Assign this number a nonsense number to start.
var outsideNumber = 0; // Assign this number a nonesense number to start.
var answer = insideNumber / outsideNumber;
function getNewNumber()
{
insideNumber=Math.round(81*Math.random()); // assign insideNumber to a random number no greater than 81
outsideNumber=Math.round(9*Math.random()); // assign outsideNumber to a random number no greater than 9
showMathProblem();
}
function showMathProblem()
{
if(insideNumber==0 || outsideNumber==0 || outsideNumber>insideNumber || answer>9 || !(insideNumber%outsideNumber)==0) // rules that the problems need to follow
{
getNewNumber();
}
else
{
document.getElementById('mathConsole').innerHTML= "insert HTML to form the math problem here"
}
}
Basically, all I get is a "too many recursions" error in Firefox. Internet Explorer just locks. Of course, this error makes sense, because it's just going to pass back and forth until the numbers follow the rules. This could take a few attempts - most of the times, more attempts than what the browsers allow.
My question is: what better way is there to do this (try to assign my variables the appropriate numbers before going on to display the math problem)?
Any help would be very much appreciated!
Basically, I have three variables: insideNumber, outsideNumber, and answer. To be sure my division problems will make sense to a 3rd grader, I have some basic rules these variables need to follow. To make the math problems follow these rules, I need some sort of logic to discard the numbers assigned to these variables, until the problem is appropriate for my 3rd grader. Once the problem is appropriate, I need the numbers to be displayed on the screen. I may be going about it all wrong, but so far, what I have is as follows:
var insideNumber = 0; // Assign this number a nonsense number to start.
var outsideNumber = 0; // Assign this number a nonesense number to start.
var answer = insideNumber / outsideNumber;
function getNewNumber()
{
insideNumber=Math.round(81*Math.random()); // assign insideNumber to a random number no greater than 81
outsideNumber=Math.round(9*Math.random()); // assign outsideNumber to a random number no greater than 9
showMathProblem();
}
function showMathProblem()
{
if(insideNumber==0 || outsideNumber==0 || outsideNumber>insideNumber || answer>9 || !(insideNumber%outsideNumber)==0) // rules that the problems need to follow
{
getNewNumber();
}
else
{
document.getElementById('mathConsole').innerHTML= "insert HTML to form the math problem here"
}
}
Basically, all I get is a "too many recursions" error in Firefox. Internet Explorer just locks. Of course, this error makes sense, because it's just going to pass back and forth until the numbers follow the rules. This could take a few attempts - most of the times, more attempts than what the browsers allow.
My question is: what better way is there to do this (try to assign my variables the appropriate numbers before going on to display the math problem)?
Any help would be very much appreciated!