7FuncObjII - JavaScript Global Functions and Objects II...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 7. Functions and Objects II - 1 http://www.cs.cityu.edu.hk/~helena JavaScript JavaScript Global Functions and Objects Global Functions and Objects II II Array String Document and Adding Cookies Forms Style, Cascading Style Sheets (CSS) and Dynamic HTML (DHTML)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(CS1301) Introduction to Computer Programming City Univ of HK / Dept of CS / Helena Wong 7. Functions and Objects II - 2 http://www.cs.cityu.edu.hk/~helena Array Objects Arrays - An array is a collection of data values. Name of array 56 70 80 51 72 c[ 0 ] c[ 1 ] c[ 2 ] c[ 3 ] c[ 4 ] index of the data value within the array va r c ; c  = ne w Arra y(5); c [0]=56; c [1]=70; c [2]=80; c [3]=51; c [4]=72; a le rt("The  3rd va lue  is  "  + c [2]); Each data value in an array has an index • The first data value is the 0 th value (its index is 0) To create a new array, we can specify the size as the argument for the Array constructor: va r c  = ne w Arra y(5); c contains 5 values, with indexes : 0, 1, 2, 3, 4 To access a specific value in an array, we write: array_name [ index_of_value] Why do we need Arrays? • Allow us to use one single name to refer to all values in the collection. Easy to handle the values in the collection.
Background image of page 2
City Univ of HK / Dept of CS / Helena Wong 7. Functions and Objects II - 3 http://www.cs.cityu.edu.hk/~helena Array Objects Example 1: To hold 5 marks /* [METHOD 1] Not using array */ var mark1,mark2,mark3,mark4,mark5; mark1=60; mark2=70; mark3=85; mark4=68; mark5=90; /* [METHOD 2] Using array */ var marks=new Array(5); marks[0]=60; marks[1]=70; marks[2]=85; marks[3]=68; marks[4]=90; Need five variable names Only need one array name Example 2: To display five hundred marks /* [METHOD 1] Marks stored in 500 variables : var mark1, mark2, mark3, mark4, . . ; */ document.write(mark1+"<br/>"); document.write(mark2+"<br/>"); document.write(mark3+"<br/>"); document.write(mark4+"<br/>"); document.write(mark5+"<br/>"); document.write(mark6+"<br/>"); document.write(mark7+"<br/>"); document.write(mark8+"<br/>"); document.write(mark9+"<br/>"); document.write(mark10+"<br/>"); document.write(mark11+"<br/>"); /* [METHOD 2] All marks in One array : var marks=new Array(500); */ var i; i=0; while (i<500) { document.write(marks[i]+"<br/>"); i++; } 500 lines of document.write(. .)! The loop is several
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

7FuncObjII - JavaScript Global Functions and Objects II...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online