Results 1 to 4 of 4

Thread: Invoke Function Loop: This script works without a loop or while loop. explain why?

  1. #1
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default Invoke Function Loop: This script works without a loop or while loop. explain why?

    Hi all.
    Just a script i've been working on to learn local and global variables.
    I was under the impression that the only way a script could loop is by using FOR/WHILE or DO loops.

    Here i have used if/else statements and invoked a function within a function (on both functions). The consequence of this has been a continuous loop between both functions until the else statement is resolved.

    Can anyone explain why this happens. I like the results. I just need to understand why

    1. Break is not required after the else statement is satisfied to break the loops
    2. this script loops irrespective of a for/loop statement

    HERE IS THE SCRIPT:

    Code:
    <script type="text/javascript">
    /*
    	***** RANDOM COUNTER FIVE *****
    this random counter is programmed with local variables within one function:
    var	- number	-	is a GLOBAL variable of 	-	random_counter()
    
    The declaration of a GLOBAL variable as the value of the random math function means
    The result is that:
    	- random_counter5()
    			declares a new random value that is passed onto 
    				- declare_global5()
    					the functions loop until the else statement is satisfied
    						
    */
    
    //random counter 20
    function random_counter5()	
    
    	{
    
    number5 = Math.floor(Math.random()*20) +1
    document.write('random internal ' + number5 + '<br />');
    declare_global5()
    	}
    
    function declare_global5()	
    
    	{
    if (number5>=1 && number5<=19)	{
    document.write(' global internal ' + number5 + '<br />');
    random_counter5();
    							}
    else	{
    if (number5==0 || number5==20)
    			{
    			alert("The number reached has been a zero or twenty");
    			}
    		}
    
    	}
    
    </script>
    The script is called via a form button onclick event handler.

    Code:
    <form>
    <input type="button" value="random counter five" onclick="random_counter5();">
    </form>

    you can find my scripts at [removed link by request]
    Last edited by Snookerman; 05-11-2010 at 09:47 AM. Reason: removed link by request

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,136
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    A loop is simply a process that continues until stopped. It doesn't require a for, a do while, or a while.

    You can make a loop in a number of ways, this is just another way - one function calling another which calls the first function until a condition is satisfied.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2009
    Location
    Sydney, Australia
    Posts
    110
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by jscheuer1 View Post
    one function calling another which calls the first function until a condition is satisfied.
    thanks for the reply. i'm still confused though because in my script, the condition is satisfied when:

    Code:
    if (number4>=1 && number4<=19)
    is TRUE (if a random number is between 1-19)
    and then writes the number to the screen
    and then calls back to the original function

    Sorry if i seem naive, but there must be several conditions in the script, which one specifically causes the invoke function loop??

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,136
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    Just follow the logic. There is no way to know for sure what number will be generated in any given cycle. As long as the number generated satisfies:

    Code:
    number5>=1 && number5<=19
    (1 to 19) the loop will continue.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    sniperman (06-28-2009)

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
  •