1 / 14

Arrays

Arrays. What are arrays? Examples Defining an array Using an array Notes about arrays Records MiniLab #4. What Are Arrays?. Lists of similar things. Way to group items for easy access. Definition - an indexed collection of objects of any type. Examples. List of numbers List of names

Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays • What are arrays? • Examples • Defining an array • Using an array • Notes about arrays • Records • MiniLab #4

  2. What Are Arrays? • Lists of similar things. • Way to group items for easy access. • Definition - an indexed collection of objects of any type.

  3. Examples • List of numbers • List of names • List of student records • List of candy available • List of CD information • List of classes to take • … many more!

  4. Defining an Array in JavaScript • JavaScript has built-in help with arrays. • Easiest way to define an array is with a function. • this refers to the array before it’s been given a name.

  5. Defining an Array - Example function makeColorArray() {this.length = 7;this[0] = "red";this[1] = "orange";this[2] = "yellow";this[3] = "green";this[4] = "blue";this[5] = "indigo";this[6] = "violet"; }

  6. Using an Array • The array isn't really created until you call the function. • var colors = new makeColorArray(); • Now colors is an array of color names.

  7. Notes About Arrays • You can refer to the length of an array by using name.Length. For example, colors.lengthrefers to the length of the color array which is 7. • You may use colors.length anywhere you would use the number of items in the array. That way if you change the number of items in the array, it’s updated for you automatically everywhere in your program.

  8. Notes About Arrays • If the number of elements in the array is n, then the elements are numbered 0 through n - 1 • To access an item in an array use the brackets around a number. • For example, colors[0]is the first element in the array. And colors[colors.length-1]is the last element in the array.

  9. Arrays of Records • Records are used to group several, possibly dissimilar, pieces of information into a single unit. • You create records using the new command and a function. • Remember that all functions reside in between the <HEAD> and </HEAD> tags in your web page.

  10. Arrays of Records - Example Create an array to contain both the name and phone number of each student in our class. function makeStudentRecord (studentName, studentPhone) { this.name = studentName; this.phone = studentPhone; }

  11. Arrays of Records - Example function makeStudentArray () { this.length = 2; this[0] = new makeStudentRecord (“Kelly”, “337-5721”); this[1] = new makeStudentRecord (“Pam”, “337-7064”); }

  12. Using a Record • Somewhere in our web page we must still create the array. • var StudentArray = new makeStudentArray(); • Now StudentArray is an array of records that contain a student name and a student phone number.

  13. Notes About Records • You must still access the items in the array with brackets but now there is an added way to get to the record parts. • For example, StudentArray[0]is the first element in the array but StudentArray[0].name is the name part of the record. • And StudentArray[0].phoneis the phone part of the first element.

  14. MiniLab #4 • You will be creating an array of records of your own. The items in the array can be the same as in the lab or you can be creative and make up your own items. • document.writeln(“<strong>Name: “ + studentArray[0].name + “</strong>”); will print the name of the first student in your array onto your web page in bold.

More Related