Results 1 to 5 of 5

Thread: document.write looping through random numbers

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    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
    pnj

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    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>
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2000
    Posts
    537
    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.
    pnj

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    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
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    to generate a random integer:
    Code:
    function getRandNum(MaxNum)
    {
    return((Math.floor(Math.random()* MaxNum))+1)
    
    }
    Mark
    -------------------

Posting Permissions

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



Click Here to Expand Forum to Full Width