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:
The script is called via a form button onclick event handler.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>
Code:<form> <input type="button" value="random counter five" onclick="random_counter5();"> </form>
you can find my scripts at [removed link by request]![]()



Reply With Quote


Bookmarks