Website Development I

Course Info Class Notes Resources Student Websites

Class No. 17:

Structuring Data with Arrays

Today’s Goal

To create a page that is populated dynamically from an array.


Class Resources

Web Programming book reading for THIS class

Web Programming book reading for NEXT class

Same as this class.


Preambles

Enhanced version of the function demonstration progrm introduced in our last class

Questions from our last class or the readings or work between classes?


Array Definition

A First Example

var arrExample = [ 29, 17, 42, 13, 56 ] ;
document.writeln( "<p>arrExample[3] = " + arrExample[3] + "</p>" ) ;

What will this line display on the screen?


Declaring and Initializing an Array

Technique #1

var arrWeekday = [ "Sunday", "Monday", "Tuesday" ] ;

Technique #2

var arrWeekday = [] ;
arrWeekday[0] = "Sunday" ;   // remember that array indices start at 0
arrWeekday[1] = "Monday" ;
arrWeekday[2] = "Tuesday" ;

Technique #3

var arrWeekday = new Array() ;
arrWeekday[0] = "Sunday" ;   // remember that array indices start at 0
arrWeekday[1] = "Monday" ;
arrWeekday[2] = "Tuesday" ;

Technique #4

var arrWeekday = new Array() ;
arrWeekday.push( "Sunday" ) ;
arrWeekday.push( "Monday" ) ;
arrWeekday.push( "Tuesday" ) ;

Note that the use of “arr” before the array name follows the Hungarian notation naming convention that we discussed in our very first JavaScript class


Using an Array


Array Properties and Functions


Today’s Exercise

Goal:  To work with JavaScript arrays.

Procedure:

  1. As we did in our last class, I recommend that you start with a fresh page for this exercise.  So start by creating a new HTML file and populating it with the HTML5 template we’ve been using all along.
  2. First, create an array that contains any data that you like, but the data items should be related to each other in some way.  For example, you might create an array of the last names of the men in your unit.
  3. Display the contents of your array using a for loop.  Each element that you print should be on a separate line.
  4. Next, create a second array in which each data item is related to the correspondingly numbered data item in the first array.
  5. Now display the data again, but this time format it into a table with the elements in the first array in the first column and the elements in the second array in the second column.


 


This is Class No. 17.  It was last modified on Saturday, October 21, 2023 at 4:38 PM.  Copyright © 2010-2024 by Jesse M. Heines.  All rights reserved, but may be freely copied or excerpted for educational purposes with credit to the author.