-
hello,
I have a function that creates two random numbers and I want to display them to the user like this:
numA + numC = [input box & submit button]
this is not a problem but I want to display five of these in a row. I can make a counter but I can't get it to work in netscape.
here is what my code looks like at this moment.
+++++++++++++++++++++++++++++++++++
<script>
function random(maxValue) {
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*3600)+(min*60)+(sec)+mili) % maxValue);
}
function ranom(maxValue) {
day= new Date();
mil=day.getTime();
return((mil) % maxValue);
}
function add()
{
maxValue=100;
numA=random(maxValue);
numB=ranom(maxValue);
numC=numA + numB;
alert(numA + " " + numC);
for (var counter=0; counter<5; counter++)
{
document.write ("<p>" + numA + "+" + numC + "=" +"<br>"+ "</p>");
add()//I tried this for netscape but it doesn't work
}
//alert(counter)
}
</script>
<form name="daForm">
<input type="button" value="submit" onClick="add();">
</form>
++++++++++++++++++++++++++
this works in IE if I take out the line that calls add() after document.write
other wise it loops continually and I have to ctrl alt delete to exit the browser.
any help on this is greatly apreciated.
thanks
-
Hi pnj,
The reason its not working in Netscape is you have a endless loop in your code. When you call the add() function it calls itselfagain(recurse) making it an endless loop.
Secondly when you try the following line in a loop in Netscape for some reason it gives you an error message which is a mystery to me, may be someone else can explain it. But i found a way round it by using a string to store the html code and then writing the string.
Code:
document.write ("<p>" + numA + "+" + numC + "=" +"<br>"+ "</p>");
Here is the modified code which works both in IE and Netscape. Hope this helps
Code:
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language=javascript>
function random(maxValue)
{
day= new Date();
hour= day.getHours();
min=day.getMinutes();
sec=day.getSeconds();
mili=day.getTime()
return(((hour*3600)+(min*60)+(sec)+mili) % maxValue);
}
function ranom(maxValue)
{
day= new Date();
mil=day.getTime();
return((mil) % maxValue);
}
function add()
{
var numA;
var numB;
var numC;
var maxValue=100;
var str="";
for (var i=0;i<5;i++)
{
numA=random(maxValue);
numB=ranom(maxValue);
numC=numA + numB;
str=str+"<p>" + numA + "+" + numC + "=" +"<br>"+ "</p>";
}
document.write (str);
}
</script>
</HEAD>
<BODY>
<form name="daForm">
<input type="button" value="submit" onClick="add();">
</form>
</BODY>
</HTML>
-
thanks Danial,
this works great.
is there a way to make the five lines have different numbers?
right now it only seems to call the random function once and then display those numbers five times.
I would like it to change the numbers on every line.
like this
12 + 3=
2 + 33=
35 + 9=
7 + 2=
8 + 5=
does that make sense?
thanks again for your help.
-
Hi PNJ,
Yes i think there is a way to make the numbers completly random. I am at the uni now when i get back i will post a reply
-
to generate a random integer:
Code:
function getRandNum(MaxNum)
{
return((Math.floor(Math.random()* MaxNum))+1)
}