8.1  Java Script - One-Dimensional Arrays 

Index Code Tester

OBJECTIVE

DISCUSSION

Array Fundamentals 

1D array

Index Item
0 Jo
1 Bob
2 Skippy
... ...
N Bif

 2D array

0 1 2 ... N
0 a b c ... d
1 e f g ... h
2 i j k ... l
... ... ... ... ... ...
M m n o ... p

Using Arrays

var sNames = new Array()   //creates with 0 elements

var iNumbers = new Array(4)  //array with 4 elements (0 to 3)

var fVars = new Array(3.14158, 6.02, 2.54)  //3 elements

sNames[0] = "Jo"

iNumbers[3] = 4

fVars[1] = 6.023

alert(iNumbers[3])

var sQ = sNames[0]

alert(sNames[iNumbers[3])

Array Methods and Properties

var myArray = new Array(3,11,4,2)

myArray.sort()   //Arranges elements as 11,2,3,4

var myArray = new Array("c", "a", "b")

alert(myArray.length)  //will display 3

EXERCISES

1.  Write statements to accomplish the following:

  1. Declare an array called iA to hold 5 numbers.
  2. Declare an array called iB to hold the numbers 1, 3, and 4.
  3. Declare an array called sC to hold the strings "1", "2", and "4".
  4. Declare an array called fD. You don't know how many items will be in the array.
  5. Assign 5 to the very first element in array iB in b above.
  6. Assign 10 to the very last element in array iB in b above.
  7. Assign "a" and "b" to the array fD in d.
  8. Display the 2nd element of array sC in an alert.
  9. Display the length of array sC in an alert.
  10. Sort array sC.

2.  In this exercise you will experiment with an array. 

  1. Create a page with a button. 
  2. Add a script block in the head section.
  3. Define a global array called myArray.  Do not specify how many items it will hold.
  4. Define a function called display().
  5. In the function, add one line of code to display the length of myArray in an alert.  Also add one line to display the last element in the array using alert(myArray[myArray.length - 1]).
  6. Run the program.  What is the length of myArray? What is the last element in the array?
  7. Modify the declaration of myArray so that it holds 4 elements.  Then run the program.  What is the length of the array?  What is the last element?
  8. Add a line of code to set the last element to "a".  Run and test the program. 
  9. Comment out the line you added in h.
  10. Modify the declaration of myArray so that it contains "a", "b", and "c".
  11. Run and test the program. How many elements does it have?  What is the last element?
  12. Add a statement to add "d" to the array.  Run the program.  How many elements does it have and what is the last element?

3.  Create a page that allows the user to add items to an array, display all of the items, display the length of the array, and sort the array. Hint:  To display the array you might want to create a global variable gCounter to keep count of the number of clicks of the Display button.  Then when the user clicks the Display button you can increment gCounter and display the element in the array at position gCounter.

LINKS AND HELP

http://javascript.about.com/library/weekly/aa111201a.htm