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

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

Dec 14, 2015

Download

Documents

Karin Wilson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.

Project 1: Using Arrays and Manipulating Strings

Essentials for DesignJavaScript

Level TwoMichael Brooks

Page 2: Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael 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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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