Week 4
Week 4
Week 4 Arrays
1
if…else --?-- switch
• If the action to be taken of the value of a single
variable (or a single expression), use ‘switch’
2
Compound Statements
• At times, we need to put multiple statements at
places where JavaScript expects only one
x=1;
Initial count
while ( x < 6000 ) {Condition Operation
document.write ( x ) ;
x=x+1;
} for ( x = 1 ; x < 6000 ; x = x + 1 ) {
document.write ( x ) ;
}
4
for --?-- while
• When the exact number of iterations is
known, use the ‘for’ loop
5
‘for’ loops become especially useful
when used in conjunction with arrays
6
Today’s Topic:
Arrays
7
Array?
8
Array
11
Array
fruit[ 0 ]
Square
Identifier Index
bracket
12
Let’s now take look at one of the
advantages of using arrays
13
var student1, student2, student3, student4 ;
student1 = “Waseem” ;
student2 = “Waqar” ;
student3 = “Saqlain” ;
student4 = “Daanish” ;
document.write( student1 ) ;
document.write( student2 ) ;
document.write( student3 ) ;
document.write( student4 ) ; 14
student = new Array( 4 ) ; //array declaration
student[ 0 ] = “Waseem” ;
Can you see
student[ 1 ] = “Waqar” ; the advantage
student[ 2 ] = “Saqlain” ; of using arrays
student[ 3 ] = “Daanish” ; along with the
‘for’ loop?
for ( x = 0 ; x < 4 ; x = x + 1 ) {
document.write( student[ x ] ) ;
}
15
Arrays in JavaScript
• In JavaScript, arrays are implemented in the
form of the ‘Array’ object
17
The ‘new’
This is the operator creates Pair of
identifier of the an instance paren-
new instance theses
19
‘Instances’ of an Object
20
All instances
of an object are
objects themselves!
21
‘Property’ Values of the Instances May Differ
22
student = new Array( 4 )
23
Array Identifiers
The naming rules for Array identifiers are
the same as were discussed for variable
identifiers
24
Assigning Values to Array Elements
a[ 1 ] = 5 ; //the second element
name[ 5 ] = “bhola” ;
number = 5 ;
name[ number ] = name[ 5 ] ;
for ( x = 0 ; x < 10 ; x = x + 1 ) {
y[ x ] = x * x ;
} 25
Remember: just like C, C++ and Java,
the first element of an array has an
index number equal to zero
26
JavaScript Arrays are Heterogeneous
Unlike many other popular languages,
a JavaScript Array can hold elements
of multiple data types, simultaneously
a = new Array( 9 ) ;
b = new Array( 13 ) ;
b[ 0 ] = 23.7 ;
b[ 1 ] = “Bhola Continental Hotel” ;
b[ 2 ] = a ; 27
The ‘length’ Property of Arrays
‘d’ is an ‘length’ is a
instance of the property of the
‘Array’ object object ‘d’
d = new Array ( 5 ) ;
document.write( d.length ) ;
28
The ‘length’ Property of Arrays
x = new Array ( 10 ) ; What is
advantage
for ( x = 0 ; x < 10 ; x = x + 1 ) { of using
‘x.length’
y[ x ] = x * x ;
here
} instead of
x = new Array ( 10 ) ; using the
literal ‘10’?
for ( x = 0 ; x < x.length ; x = x + 1 ) {
y[ x ] = x * x ;
} 29
Array Methods: sort( )
Sorts the elements in alphabetical order
31
Array Methods: reverse( )
Reverses the order of the elements
x = new Array ( 4 ) ; Saqlain
x[ 0 ] = “Waseem” ; Shoaib
x[ 1 ] = “Waqar” ; Waqar
x[ 2 ] = “Saqlain” ; Waseem
x[ 3 ] = “Shoaib” ; Is this the
x.reverse( ) ; required
x.sort( ) ; result?
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>”) ;
} 32
Array Methods: reverse( )
Reverses the order of the elements
x = new Array ( 4 ) ; Waseem
x[ 0 ] = “Waseem” ; Waqar
x[ 1 ] = “Waqar” ; Shoaib
x[ 2 ] = “Saqlain” ; Saqlain
x[ 3 ] = “Shoaib” ;
x.sort( ) ;
x.reverse( ) ;
for ( k = 0 ; k < x.length; k = k + 1 ) {
document.write( x[ k ] + “<BR>”) ;
} 33
Let’s Now Do a More Substantial Example
35
36
37
Pseudo Code
1. Declare the array that will be used for
storing the words
41
Pseudo Code
1. Declare the array that will be used for
storing the words
enter data
Pseudo Code
1. Declare the array that will be used for
storing the words
45
Pseudo Code
1. Declare the array that will be used for
storing the words
47
During Today’s Lecture …
• We found out why we need arrays
48
Next Web Lecture:
Functions & Variable Scope
• To become familiar with some of JavaScript’s
built-in functions