project 1: using arrays and manipulating strings essentials for design javascript level two michael...

11
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks

Upload: karin-wilson

Post on 14-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Project 1: Using Arrays and Manipulating Strings

Essentials for DesignJavaScript

Level TwoMichael Brooks

Copyright (c) 2005 Prentice-Hall. All rights reserved. 2

Objectives

Use string methods Create and populate an array Sort an array Incorporate array methods Split and join strings Use multi-dimensional arrays

Copyright (c) 2005 Prentice-Hall. All rights reserved. 3

Why Would I Do This?

String object – holds text Common example – a variable that holds text

String method – used to manipulate string object

Array – holds multiple items of data Element – an individual piece of data Multidimensional array – an array stored as an

element of another array

Copyright (c) 2005 Prentice-Hall. All rights reserved. 4

Using String Methods

toUpperCase() – returns string in all uppercase letters

charAt() – returns a single character indexOf() – returns the position of a substring substring() – extracts a substring from

another string

Copyright (c) 2005 Prentice-Hall. All rights reserved. 11

Creating and Populating Arrays

Each element in array represented by an index, placed within a bracket window[1] refers to the second element in the

“window” array Array() Constructor method Using numbered array elements Applying named array elements

Copyright (c) 2005 Prentice-Hall. All rights reserved. 16

Sorting Arrays

sort() method reorders the contents of an array By default, sorted in ascending order Custom sort order can be specified

Older browsers may require that a sort order be specified

Copyright (c) 2005 Prentice-Hall. All rights reserved. 17

Incorporating Array Methods

pop() method returns last element in array Also removes last element from array

push() method adds element to end of array reverse() method reverses the order of all

elements in an array shift() method removes first element in array

Other elements move up one position in array unShift() method adds element before the

first element Other elements move down one position in array

Copyright (c) 2005 Prentice-Hall. All rights reserved. 18

Incorporating Array Methods (cont)

shift() method removes first element in array Other elements move up one position in array

unShift() method adds element before the first element Other elements move down one position in array

Copyright (c) 2005 Prentice-Hall. All rights reserved. 19

Splitting and Joining Strings

Delimiter – marks the beginning or end of a unit of data Can be a single character or group of characters Commas, tabs, and spaces most commonly used

split() method – splits a string into an array join() method – joins elements in an array

into a string

Copyright (c) 2005 Prentice-Hall. All rights reserved. 23

Array Terminology

Stack – last item added to array is first to be retrieved A Last In, First Out (LIFO) approach

Queue – first item added to array is first to be retrieved A First In, First Out (FIFO) approach

Copyright (c) 2005 Prentice-Hall. All rights reserved. 24

Using a Multi-Dimensional Array

An array stored within an array Useful with large chunks of data that need to

be divided into subcategories