About the array datatype in JavaScript...

We have spent some time talking about arrays, which are conceptually collections of objects (called elements) that are accessed by their "index," which is a non-negative integer. In addition, these arrays reside in memory and may be associated with user variables, as in
    var myCollection = [ 1,2,3 ];

which would declare the variable myCollection as an array containing three elements, the integers 1, 2 and 3.

We access the elements of an array by using an index in conjunction with the bracket-operators, as in

    myCollection[ 0 ] // access the first location, which is the integer 1
    // likewise, we could replace the 1 with -1 by writing:
    myCollection[ 0 ] = -1;

Note that last line of code shows that we can read and write to arrays.

Some additional, useful facts about JavaScript arrays ...

Empty brackets are used to signal to JavaScript that we are working with arrays. But, these brackets also can be interpreted as requests to either access the value of an element at a location in the array, or to access a location in the array. Again, review the example directly above.

Sometimes, we need to create a new array of a particular size: for example, if we are asked to write a function that creates arrays for any reason. The first activity that you will be asked to perform on this worksheet does just that. So, we need to ask: how do we instruct JavaScript to create a new array?

I'm glad you asked. A variety of opinions are available on this topic, but ignoring the "purists," if one knows ahead of time how large the array needs to be, one can write:

    var newArray = Array( size ); // where size is a non-negative integer.

Of course, this only creates the array. We now need to populate it, i.e., put values in its available locations; otherwise, referencing an empty location will certainly give us unexpected and unwelcome results!

We conveniently use the for statement to do this.

    for( var index=0; index < newArray.length; index++ ) {
       newArray[ index ] = ... some value that is stored at index ....
    }

As a subtle point, traditionally arrays are constrained to require that all of their elements be of the same type, e.g., we have an array of numbers or an array of strings, etc. But, scripting languages often permit a "looser" interpretation of these rules and, in the case of JavaScript, we are allowed to "mix" elements types within a single array.

Suggested activities ....

In this exercise, we will create various activities for you to practice programming with arrays.

Seaching activities
Here you are given an array and are asked to search for the existence of a particular element. Sometimes the activity requires that you simply tell the user whether or not the element exists in the array, other times, however, you may need to provide additional information, such as that element's location (index) in the array.
Filtering activities
Here you often extract a new array by collecting elements from an existing array into a new array. For example, given an array of integers, create a new array of just the even integers, leaving the original array intact.
Swapping activities
Usually this entails exchanging the position of elements within an array.

Generating an array

Begin by writing the function genArray(n), where n is a positive integer. genArray(n) creates and returns an array containing n randomly-generated, non-negative integers, for example:

    genArray( 3 );
    // creates and returns an array: [2,1,1] or any remaining possbilities.

Enter a positive integer (a whole number greater than zero but less than 100)


Searching an array

After getting the first exercise to work, write the function indexOf( target ), that examines the elements from left to right of the array you created in the previous exercise and returns the index of the parameter target. Note that if the target is not found in your array, then a -1 must be returned.

After you write your function, enter the target here and see the results of your search below; in the results, the index that your function returned is used to interrupt the printing of the array elements.


Filtering an array

Write the filterEvens() function that examines the array that you created above and creates a new array of just the "even" integers, in no particular order, that appeared in the original array. Important: your function should not modify the original array!

Make sure that you define the function filterEvens() before you click on the button below or nothing will happen.

When you're ready click


Swapping elements around within an array

Swapping elements around an array requires that you know the from index, the to index, and, of course, the array in question. For this exercise, you will define the procedure (function, in JavaScript) swap( to, from, anArray ). The test code will generate an array of an arbitrary size, display it, and the result of swapping two of its elements. Note: unlike "pure" functions, swap() is a procedure; it returns no value, but instead modifies the array that you pass into it!!

Define your swap procedure in the proper place, and then click on the button below to test it out and see the results.

When you're ready to test your function, click (If you continue clicking the button, the test code will generate new arrays, but will always use the same indices. Do this several times to make sure that your procedure works as expected.)


Sorting Elements by Selection

to invoke the SelectionSort routine on the original array (you know, the one with the randomly generated integers in step one), and display the results in the space below:


Sorting elements by Insertion

to invoke the InsertionSort routine on the original array (you know, the one with the randomly generated integers you did in step one), and display the results in the space below: