Extra Help on Arrays

I am often asked, "Does array element temp[2][3] represent the item in column 3 and row 2 or the item in column 2 and row 3?" Well, it can be either. The program doesn't consider terms such as "columns" and "rows" as it simply stores the array elements as numbers in memory.

However, you do need to be consistent! Once you have decided which dimension will be rows and which will be columns, make sure that you stick to it. If I feel that I will get confused, I tend to refer to the array elements with appropriately named variables:

row = 6;
col = 10;
total += grid[row][col];  // Should be fairly obvious

You don't have to use all the elements of an array. Just set up and use the ones that you need, and the others will be left undefined. Here's an example of an array where only elements 4 and 10 are used:

var almost_empty = new Array();
almost_empty[4] = "Hello!";
almost_empty[10] = "Hello again!";
alert(almost_empty[6]);

The alert() command tries to display the value in an array element that hasn't been set to anything. What will happen? Well, since the array element is undefined, that's exactly the value returned by almost_empty[6]:

undefined

Worse still, if you try to involve any undeclared elements as parts of arithmetic calculations, you will find that the answer comes out as the special value NaN. This isn't a numerical or string value, but is a symbol standing for "Not a Number." The following piece of code, for instance:

var another = new Array();
another[7] = 33.4;
another[3] = 20.5;
var total = 0;
for (count = 0; count < 11; count++)
 total += another[count];
alert("The elements add to give " + total);

will produce the following output:

Not a number!

So please be careful to set all the array elements that you are going to need later in the calculation.

A for loop is the obvious structure to use when plodding through an array. Also, you can use the length property of the array to work out how many elements there are in it - you don't have to remember how many there are. Beware, of course, of any NaN elements (see the point that I made above).

Here, for instance, is a loop which finds the smallest multiple of 5 in an array:

var index, smallest_so_far = 999999;
for (index = 0; index < values.length; index++)
 if (values[index] < smallest_so_far && values[index] % 5 = 0)
  smallest_so_far = values[index];

You will notice that we start with a dummy value of smallest_so_far which is so large that any multiple of 5 in the array (and probably every other number as well) is almost certainly smaller than it is. We then go through the array using a for loop, from index 0 to whatever the maximum index in the array is testing to see if we have a number which is a multiple of 5 and whether it is smallest than the current leader.

Warning!

You should be careful about using length with an array. Remember, the length of the array is one more than the highest index value in the array.

Suppose we have an array declared like this:

var temp_array = new Array();
for (x = 0; x < 10; x++)
  temp_array[x] = 23 * x;

In this case, the array elements are set up from 0 to 9 (not 10), because as soon as x reaches 10, the loop terminates and temp_array[10] is never set. This means that the value of temp_array.length is 10 (not 9).

You could do nasty things to all the elements in the array using a for loop like this:

for (a_value = 0; a_value < temp_array.length; a_value++)
 { // whatever
 }

In this case, the value of a_value starts at 0 and climbs upwards to 9. It never reaches 10, which is the value of temp_array.length, as the loop only carries on as long as a_value is less than temp_array.length. Both this loop and the one above it, in fact go through the same range of numbers in the same order.

What about counting downwards? Yes, that's no problem. This time we start at 9 (which is temp_array.length - 1) and go down to 0:

for (a_value = temp_array.length - 1; a_value >= 0; a_value--)
 { // whatever
 }

Note that we decrease a_value by 1 every time (using -- instead of ++) and the end condition includes the value 0, using >=.