Arrays


Slides

Slides Here!


Additional Info


In Class Exercises (ICEs)

Part 1: Creating an associative array of simple/primitive values

Create an associative array correctly (NOT using new Array(), or anything equivalent to that) and initialize it with the names of fictitious students & their corresponding grades.  You should set up the array so that the 'index' value is the name of the student, and the value stored there is their GPA. 

Use a for ... in loop to display everything in the array on the page.

Part 2: Creating and using an associative array of simple/primitive values

Make a copy of the prior exercise in order to build on it.

Put two textboxes and a button on the page.  When the user clicks the button the click handler should add the student (whose name is given by the first box) and GPA (in the second textbox) into the associative array, then display a list of students (and their GPAs) on the page somewhere.

Next, put another textbox and button on the page.  When the user clicks the button the page will check and see if the student (whose name was typed into the textbox ) is present in the array.  If so then a message listing the student's name and GPA will be displayed on the page.  Otherwise a message clarifying that the student is not present should be displayed.

Part 3: Creating an associative array of objects (optional)

Create a new class that will represent information about Students.  Make sure that each object will have at least 2 properties (such as name or GPA ) and at least one method (toString is fine, as long as you call it explicitly throughout the rest of this exercise)(you should add the method to the class's prototype).  If each student had only 1 property that we cared about then we could use a simple/primitive array like we did in the prior exercises.  Since each student now has multiple interesting properties we will instead use a class to package up all the interesting properties.

Next, create an associative array correctly (NOT using new Array(), or anything equivalent to that) and put some objects into it (you will need to make up values for the objects, obviously).  You should set up the array so that the 'index' value is the name of a student, and the value stored there an object representing the information about them.

Use a for ... in loop to display everything in the array on the page.

Part 4: Creating and using an associative array (optional)

Make a copy of the prior exercise in order to build on it.

Put some textboxes and a button on the page.  When the user clicks the button the click handler should create a new Student object to represent then store that object into the associative array, then display a list of students (and their GPAs) on the page somewhere.

Next, put another textbox and button on the page.  When the user clicks the button the page will check and see if the student (whose name was typed into the textbox ) is present in the array.  If so then a message listing the student's name and interesting properties will be displayed on the page (for this you should use the toString method).  Otherwise a message clarifying that the student is not present should be displayed. 

OPTIONAL Part 2: Using the switch Statement

Create a page with a textbox that allows the user to enter a month, along with a button to call a function named displayNumberOfDaysInTheMonth.  In the function, you should use a switch statement to display the number of days in the month entered by the user (using an on-page paragraph - do NOT use alert).  Use the switch's default case to provide a message when the user has not entered a valid month, and offer a different message if the user elected to cancel the prompt instead of submitting a month.

Note: one way to easily compare strings is to convert the user's input to lowercase, then use all lowercase month names in your switch's cases.  You may need to search online for a JavaScript function that converts a string to lowercase.