DiggThis

Invoke A Function From Within A Function Loops

This exercise tests the scope of local variables and global variables both inside and outside of Javascript functions.
The exercise is divided into five logical scripts with similar code but small differences in where variables are placed to demonstrate the varied outcomes.

This random counter is programmed with three types of variables.


VARIABLES

  1. local variables nested within functions
  2. global variables nested within functions
  3. global variables nested within scripts but outside of functions

Each of the five scripts also calls the same two functions.

FUNCTIONS

  1. random_counter()
    A math function is performed and its random number assigned to a local/global variable
    The random value is written to the document
    The function passes to the second function:
  2. declare_global()
    This function uses an if/else statement to check on and run one of two conditions.

CONDITIONALS

  1. If the number is between 1 and 19 run the random_counter() function again
  2. else if the number is 0 or 20 display an alert and end the script


This random counter is programmed with a local variable within the random_counter() function
The local scope of the random_counter() variable means it was not passed to the global_counter() function
The If statement in global_counter() returns FALSE because global_counter() lacks a local variable.
The ELSE statement is triggered to return TRUE because a global variable of 0 is declared.
this random counter is programmed with local variables within one function: var - number - is a local variable of - random_counter() var - 0 - global variable is assumed - declare_global() GLOBAL VARIABLE - 0 - DECLARED the math function in random_counter() declares the local variable there. This however is not passed on to the declare_global() function. The result is that: - declare_global() registers the global variable - 0 - and runs the else function irrespective of the random number. this affects all the local variables within ALL functions

This allows 20 or 0 to be passed and still does not show the alert function, so it is not passed to the conditional
this random counter is programmed with local variables within one function: var - number - is a local variable of - random_counter() var - number - is a local variable of - declare_global() var - 0 - NO GLOBAL VARIABLE DECLARED The lack of a global variable "number" means the local value is not passed to work within another function. The result is that: - declare_global() fails to regiser because the local variable - number has not been declared inside or outside the function

This runs an infinite loop. Global variable always remains 10 while the random internal variable fluctuates. However, because this random internal variable remains a local variable, the If Else statement in declare_global() does not work. this random counter is programmed with local variables within one function: var - number - is a local variable of - random_counter() var - number - is a local variable of - declare_global() var - - NO GLOBAL VARIABLE DECLARED The declaration of a local variable in both functions and the lack of a global variable "number" means The result is that: - random_counter() declares a new random value but does not pass this on to - declare_global() which always has a value of 10 declared the functions endlessly loop

This process produces a more intelligent outcome. Random internal is passed somewhere. The global internal however always remains 20. When random internal is either 0 or 20 the script ends. A for loop is also incorporated into the script. This confines the possible outcome to within 20 chances and prevents an infinite loop.
This random counter is programmed with local variables within one function: var - number3 - is a local variable of - random_counter() var - random1 - is a GLOBAL variable - assigned by local variable - number3 var - stopElse - is a local variable of - declare_global() GLOBAL VARIABLE - i=0 - DECLARED The declaration of a local variable in both functions and the lack of a global variable "number" means The result is that: - random_counter3() declares a new random value and passes this onto global variable - random1 which if it meets the conditions in global_variable() performs the functions of the if statement the functions loop breaks when either random1 >20 else statement is performed

This process produces the desired outcome. random internal is passed to global internal. The if/else conditional is functional.
this random counter is programmed with local variables within one functions: var - number - is a local variable of - random_counter() var - 0 - global variable is assumed - declare_global() GLOBAL VARIABLE - 0 - DECLARED the math function in random_counter() declares the local variable there. This however is not passed on to the declare_global() function. The result is that: - declare_global() regisers the global variable - 0 and runs the else function irrespective of the random number this affects all the local variables within ALL functions

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 the global variable however does not override any local variables of the same name within the function

 

blog comments powered by Disqus