• No results found

Lec 14 JavaScript - IV.pptx

N/A
N/A
Protected

Academic year: 2020

Share "Lec 14 JavaScript - IV.pptx"

Copied!
45
0
0

Loading.... (view fulltext now)

Full text

(1)

CS1113 Web Programming

Lecture 14

Arrays

(2)

Today’s Topic:

Arrays

We will find out why we need arrays

• We will become able to use arrays for solving

(3)
(4)

Array

An indexed list of elements

We said that a variable is a container that holds a value.

(5)

var singleValue; singleValue = 99;

var multipleValues = [ ];

multipleValues[0] = 50;

multipleValues[1] = 60;

multipleValues[2] = “Camel”;

(6)

Creating Arrays - Shorthand

var multipleValue = [50, 60, “Camel”];

[0] [1] [2]

(7)

Creating Arrays - Longhand

var multipleValue = new Array();

• var multipleValue = Array();

(8)

Size of Array

var multipleValue = new Array(5);

• We can officially created an array with size,

this case i am saying array should have 5 slots (0 through 4).

But all arrays in JavaScript are dynamic,

(9)

Array

An indexed list of elements

Example: There are many ways of assigning identifiers to the following fruit

strawberry

fruit1

fruit[ 0 ]

orange

fruit2

fruit[ 1 ]

apple

fruit3

fruit[ 2 ]

watermelon

fruit4

(10)

Array

An indexed list of elements

• fruit[ 0 ], fruit[ 1 ], fruit[ 2 ], and fruit[ 3 ] are the

elements of an array

• ‘fruit’ is the identifier for that array

• The length of the ‘fruit’ array is 4, i.e. ‘fruit’ has

(11)

Array

fruit

[

0

]

Identifier Square

(12)
(13)

var student1, student2, student3, student4 ;

student1 = “Waseem” ;

student2 = “Waqar” ;

student3 = “Saqlain” ;

student4 = “Daanish” ;

(14)

student = new Array( 4 ) ; //array declaration

student[ 0 ] = “Waseem” ;

student[ 1 ] = “Waqar” ;

student[ 2 ] = “Saqlain” ;

student[ 3 ] = “Daanish” ;

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

• The key property of the ‘Array’ object is ‘length’,

i.e the number of elements in an array

• Two of the key ‘Array’ methods are:

reverse( ) – sort( )

(16)

Declaring a New Instance of the Array Object

‘student’ is an instance of the ‘Array’ object

• ‘student’ was declared such that it is of length ‘4’

• That is, student is an array having 4 elements

The four elements of the array are: ‘student[ 0 ]’,

(17)

17

student

= new Array(

4

)

This is the

identifier of the new instance

The ‘new’

operator creates an instance

This is the parent object (or class) of the new instance

(18)
(19)

Array Identifiers

(20)

Assigning Values to Array Elements

a[ 1 ] = 5 ; //the second element

name[ 5 ] = “akash” ;

number = 5 ;

name[ number ] = name[ 5 ] ;

(21)

Remember: just like C, C++ and Java,

the

first element

of an array has an

(22)

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( 11 ) ;

(23)

The ‘length’ Property of Arrays

d = new Array ( 5 ) ;

document.write( d.length ) ;

‘d’ is an

instance of the ‘Array’ object

(24)

The ‘length’ Property of Arrays

x = new Array ( 10 ) ;

for ( x = 0 ; x < 10 ; x = x + 1 ) {

y[ x ] = x * x ; }

x = new Array ( 10 ) ;

for ( x = 0 ; x < x.length ; x = x + 1 ) {

(25)

If you change the array length from 10 to

50, in first code you just do changing in two places, while in second code you will do

changing in one place.

• When you modify code there are big

(26)

Array Methods:

sort( )

Sorts the elements in alphabetical order

x = new Array ( 4 ) ;

x[ 0 ] = “Waseem” ;

x[ 1 ] = “Waqar” ;

x[ 2 ] = “Saqlain” ;

x[ 3 ] = “Shoaib” ;

x.sort( ) ;

(27)

Were the elements sorted in

ascending or descending

order?

(28)

Array Methods:

reverse( )

Reverses the order of the elements

x = new Array ( 4 ) ;

x[ 0 ] = “Waseem” ;

x[ 1 ] = “Waqar” ;

x[ 2 ] = “Saqlain” ;

x[ 3 ] = “Shoaib” ;

x.reverse( ) ;

x.sort( ) ;

(29)

Array Methods: reverse( )

Reverses the order of the elements

x = new Array ( 4 ) ;

x[ 0 ] = “Waseem” ;

x[ 1 ] = “Waqar” ;

x[ 2 ] = “Saqlain” ;

x[ 3 ] = “Shoaib” ;

x.sort( ) ;

x.reverse( ) ;

for ( k = 0 ; k < x.length; k = k + 1 ) { document.write( x[ k ] + “<BR>”) ;

}

(30)

Array Methods:

join()

Var multipleValues = [10, 20, 30, 40, 50];

var reverseVal = multipleValues.reverse(); document.write( reverseVal.join() );

(31)

31

Let’s Now Do a More Substantial Example

Develop a Web page that prompts the

user for 10 words, and then displays them in form of a list in two different ways:

1. In the order in which the words were entered

2. In a sorted order

(32)

Before looking at code, let’s first

understand what is that code

(33)
(34)
(35)

Pseudo Code

1. Declare the array that will be used for storing the words

2. Prompt the user and read the user input into the elements of the array

3. Now write the array to the document

4. Sort the array

(36)

<HTML>

<HEAD>

<TITLE>Sort Ten Words</TITLE> <SCRIPT type=“text/JavaScript”>

words = new Array ( 10 ) ;

for ( k = 0 ; k < words.length ; k = k + 1 ) {

words[ k ] = window.prompt( "Enter word # " + k, "" ) ; }

document.write( "UNSORTED WORDS:" + "<BR />" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) {

document.write( words[ k ] + "<BR />" ) ; }

words.sort( ) ;

document.write( "SORTED WORDS:" + "<BR />" ) ; for ( k = 0 ; k < words.length ; k = k + 1 ) {

(37)

<!DOCTYPE html> <HTML>

<HEAD>

<TITLE>Sort Ten Words</TITLE> <SCRIPT type=“text/JavaScript”>

//JavaScript Code

</SCRIPT> </HEAD>

(38)

The next three slides show the

(39)

Pseudo Code

1. Declare the array that will be used for storing the words

2. Prompt the user and read the user input into the elements of the array

3. Now write the array to the document

4. Sort the array

(40)

words = new Array ( 10 ) ;

for ( k = 0 ; k < words.length ; k = k + 1 ) {

words[ k ] = window.prompt(

"Enter word # " + k, "" ) ; }

This method is used for collecting data

(41)

Pseudo Code

1. Declare the array that will be used for storing the words

2. Prompt the user and read the user input into the elements of the array

3. Now write the array to the document

4. Sort the array

(42)

document.write( "Unsorted Words:" + "<BR>" ) ;

for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ;

(43)

Pseudo Code

1. Declare the array that will be used for storing the words

2. Prompt the user and read the user input into the elements of the array

3. Now write the array to the document

4. Sort the array

(44)

words.sort( ) ;

document.write( "Sorted Words:" + "<BR>" ) ;

for ( k = 0 ; k < words.length ; k = k + 1 ) { document.write( words[ k ] + "<BR>" ) ;

(45)

During Today’s Lecture …

We found out why we need arrays

• We became able to use arrays for solving

References

Related documents

4.1 The Select Committee is asked to consider the proposed development of the Customer Service Function, the recommended service delivery option and the investment required8. It

In summary and taking into account the resonance characteristics of the ACUREX plant, the main contribution of this paper, is to improve a gain schedul- ing (GS) predictive

more than four additional runs were required, they were needed for the 2 7-3 design, which is intuitive as this design has one more factor than the 2 6-2 design

• Follow up with your employer each reporting period to ensure your hours are reported on a regular basis?. • Discuss your progress with

Paragraph 1904.4(a) of the final rule mandates that each employer who is required by OSHA to keep records must record each fatality, injury or illness that is work-related, is a

The corona radiata consists of one or more layers of follicular cells that surround the zona pellucida, the polar body, and the secondary oocyte.. The corona radiata is dispersed

our options. This gives you clear water, so you are not riding the wakes of the other boats, and you get clear air. When it got light and lumpy we had the space to put the bow

The PROMs questionnaire used in the national programme, contains several elements; the EQ-5D measure, which forms the basis for all individual procedure