unit 5 functions, objects, conditions, and loops

47
UNIT 5 FUNCTIONS, O BJECTS, CONDITIONS, AND LOOPS

Upload: victor-pearson

Post on 28-Dec-2015

231 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

UNIT 5

FUNCTIO

NS,

OB JE

CTS ,

CON

DIT

ION

S, AN

D L

OO

PS

Page 2: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

OBJECTIVES CO6 Create a dynamic website using

JavaScript.

2

Page 3: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

LEARNING OUTCOMESLO25 Create a JavaScript function.

LO26 Create custom objects.

LO27 Use and extend built-in objects.

LO28 Write code that uses conditional statements.

LO29 Write code that uses loops.

3

Page 4: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

FUNCTION DEFINITION

function Greet() {

alert("Greetings.");

}

4

Page 5: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

FUNCTION WITH PARAMETERS

function Greet(who){

alert("Greetings," + who);

}

5

Page 6: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

FUNCTION IN A <HEAD><!DOCTYPE html ><head><title>Functions</title><script type="text/javascript">function Greet(who){alert("Greetings," + who);

}</script></head></html>

6

Page 7: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CALLING THE FUNCTION

<body><h1>Function Example</h1><p>Prepare to be greeted twice.</p><script type="text/javascript">Greet("Fred");Greet("Ethel");</script></body>

7

Page 8: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

GREET FUNCTION OUTPUT

8

Page 9: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

A FUNCTION THAT RETURNS A VALUE<head><title>Function Example: Average</title><script type="text/javascript">function Average(a,b,c,d){result = (a + b + c + d)/4;return result;

}</script></head>

9

Page 10: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CALLING THE AVERAGE FUNCTION<body><h1>Function Example: Average</h1><p>The following is the result of the function call.</p>

<script type="text/javascript">score = Average(3,4,5,6);document.write("The average is:" + score);

</script></body>

10

Page 11: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

TYPES OF OBJECTS

11

Built-in objects

DOM objects

Custom objects

Page 12: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CREATING OBJECTS

scores = new Array(4);

12

Page 13: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

PROPERTIES A variable stored within the object

test = "This is a test.";document.write(test.length);

Each property has a valuelocation.href=

"http://www.google.com/";

13

Page 14: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

METHOD

Function that works with the object's data

location.reload();

14

Page 15: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

USING CUSTOM OBJECTS Name the object

Identify its properties

Create a constructor

Define object methods

Create an instance of an object

15

Page 16: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

OBJECT CONSTRUCTORfunction Card(name,address,work,home) {

this.name = name;

this.address = address;

this.workphone = work;

this.homephone = home;

}

16

Page 17: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

DEFINING METHODS Write a function

Add the function definition to the constructor

17

Page 18: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

WRITE A FUNCTIONfunction PrintCard() {line1 = "Name: " + this.name + "<br/>\n";line2 = "Address: " + this.address + "<br/>\n";

line3 = "Work Phone: " + this.workphone + "<br/>\n";

line4 = "Home Phone: " + this.homephone + "<hr/>\n";

document.write(line1, line2, line3, line4);

}

18

Page 19: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

ADD THE FUNCTION DEFINITION TO THE CONSTRUCTORfunction Card(name,address,work,home) {

this.name = name;this.address = address;this.workphone = work;this.homephone = home;this.PrintCard = PrintCard;}

19

Page 20: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CREATING AN OBJECT INSTANCE

tom = new Card("Tom Jones",

"123 Elm Street", "555-1234", "555-9876");

20

Page 21: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

ASSIGN PROPERTY VALUES AFTER OBJECT CREATION

holmes = new Card();

holmes.name = "Sherlock Holmes";

holmes.address = "221B Baker Street";

holmes.workphone = "555-2345";

holmes.homephone = "555-3456";

21

Page 22: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

EXTENDING BUILT-IN OBJECTS

Use the prototype keyword to add a property or method to a built-in object.

22

Page 23: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

ADDING A METHOD TO THE STRING OBJECT<script type="text/javascript">function addhead (level) {html = "h" + level;text = this.toString();start = "<" + html + ">";stop = "</" + html + ">";return start + text + stop;}String.prototype.heading = addhead;document.write ("This is a heading 1".heading(1));document.write ("This is a heading 2".heading(2));document.write ("This is a heading 3".heading(3));</script>

23

Page 24: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

MATH OBJECT Math.ceil() rounds a number up to the

next integer.

Math.floor() rounds a number down to the next integer.

Math.round() rounds a number to the nearest integer.

24

Page 25: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

ROUNDING EXAMPLE

function round(num) {

return Math.round(num * 100) / 100;

}

25

Page 26: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

GENERATING RANDOM NUMBERSfunction rand(num) {

return Math.floor(Math.random() * num) + 1;

}

26

Page 27: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

WITH KEYWORD

with (lastname) {

window.alert("length of last name: " + length);

capname = toUpperCase();

}

27

Page 28: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

DATE FORMATS

birthday = new Date();

birthday = new Date

("November 1, 2010 08:00:00");

birthday = new Date(11,1, 2010);

birthday = new Date(11,1,2010, 8, 0, 0);

28

Page 29: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

DATE OBJECT SET METHODS setDate()

setMonth()

setFullYear()

setTime()

setHours()

setMinutes

setSeconds()

29

Page 30: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

getDate()

getMonth()

getFullYear()

getTime()

getHours()

getMinutes()

getSeconds()

getMilliseconds()

30

DATE OBJECT GET METHODS

Page 31: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

getTimeZoneOffset()

toUTCString()

toLocalString()

getUTCDate()

getUTCDay()

getUTCFullYear()

getUTCMonth()

getUTCHours()

getUTCMinutes()

getUTCSeconds()

getUTCMilliseconds()

setUTCDate()

setUTCFullYear()

setUTCMonth()

setUTCHours()

setUTCMinutes()

setUTCSeconds()

setUTCMilliseconds()

31

WORKING WITH TIME ZONES

Page 32: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CONVERTING BETWEEN DATE FORMATS Date.parse()method converts a date

string, such as November 1, 2010, to a Date object (number of milliseconds since 1/1/1970).

Date.UTC() method does the opposite. It converts a Date object value (number of milliseconds) to a UTC (GMT) time.

32

Page 33: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

IF STATEMENT

if (a == 1) window.alert("Found a 1!");

if (a == 1) {

window.alert("Found a 1!");

a = 0;

}

33

Page 34: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CONDITIONAL OPERATORS

34

Operator Meaning== Is equal to!= Is not equal to< Is less than> Is greater than

>= Is greater than or equal to<= Is less than or equal to

Page 35: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

LOGICAL OPERATORS Or

if ((phone == "") || (email == "")) window.alert("Something’s Missing!");

Andif ((phone == "") && (email == "")) window.alert("Both are Missing!");

Notif (!(phone == "")) alert("phone is OK");

35

Page 36: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

THE ELSE KEYWORD

if (a == 1) {

alert("Found a 1!");

a = 0;

} else {

alert("Incorrect value: " + a);

}

36

Page 37: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

SHORTHAND CONDITIONAL EXPRESSIONSvalue = (a == 1) ? 1 : 0;

Same asif (a == 1) {

value = 1;

} else {

value = 0;

}

37

Page 38: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

SHORTHAND EXAMPLE

document.write("Found " + counter +

((counter == 1) ? " word." : " words."));

38

Page 39: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

TESTING MULTIPLE CONDITIONSWITH IF AND ELSEif (hours < 10) {document.write("Good morning.");} else if (hours >= 14 && hours <= 17) {document.write("Good afternoon.");} else if (hours >= 17) {document.write("Good evening.");} else {document.write("Good day.");}

39

Page 40: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

TESTING MULTIPLE CONDITIONS WITH SWITCHswitch(button) {

case "next":window.location="next.html";

break;case "previous":

window.location="prev.html";break;case "home":

window.location="home.html";break;case "back":

window.location="menu.html";break;default:

window.alert("Wrong button.");} 40

Page 41: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

FOR LOOPfor (i=0; i<10; i++) {

document.write("This is line " + i + "<br />");

}

for (i=0; i<10; i++)

document.write("This is line " + i + "<br />");

41

Page 42: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

WHILE LOOP

while (total < 10) {

n++;

total += values[n];

}

for (n=0; total < 10; n++) {

total += values[n];

}

42

Page 43: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

DO...WHILE LOOPS

do {

n++;

total += values[n];

}

while (total < 10);

43

Page 44: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

INFINITE LOOP A loop that never reaches the exit

condition

while (i < 10) {n++;values[n] = 0;

}

Can make the browser stop responding

44

Page 45: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

BREAK STATEMENT

while (true) {

n++;

if (values[n] == 1) break;

}

45

Page 46: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

CONTINUE STATEMENT

for (i=1; i<21; i++) {

if (score[i]==0) continue;

document.write(

"Student number ",i,

" Score: ", score[i],

"\n");

}

46

Page 47: UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

LOOPING THROUGH OBJECT PROPERTIES

for (i in navigator) {

document.write("property: " + i);

document.write(" value: " + navigator[i] + "<br>");

}

47