Exercise Three

First, let's repeat the array and loop example from the previous section.


fred = new Array("Hi", 127, true, "Wow");

for (myCounter = 0; myCounter < fred.length; myCounter++) {

Here is this code in action.

Now, here's a real exercise:

Try instanciating three arrays in GLOBAL variables, with each array containing a different number of values (the first array might contain FOUR pieces of data, while the second array might contain FIVE pieces of data, etc). Once you have done this, see if you can create a GENERALIZED function which pulls information from ANY one of these global arrays, throwing up alert dialog boxes in the manner of the code in the FOR loop example above. Note: You may pass global variable names as arguments to your function DIRECTLY from your event handlers.

Example (for a global variable, fred, which is an instance of the Array object):

<a href="javascript:makeLoopAlert(fred);">Link Word</a>

This means that you will be creating a local argument variable within your function (I usually call mine myArray) to hold the global array which you are passing to the function. You will use this myArray local argument variable in the FOR loop above to replace the direct reference to fred itself (this is where the code will be different from the first example above).

Click here for a VERY broad hint.

Here is a possible solution to that exercise which you may analyze AFTER you have tried to write it yourself.

Main Menu