UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS.

Slides:



Advertisements
Similar presentations
Presentation 8: JavaScript continued Arrays and objects Internet Technology 1.
Advertisements

Intro to JavaScript. JavaScript History Client (generally browser-side) language invented at Netscape under the name LiveScript around 1995 Netscape wanted.
JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
ISP 121 Algorithmsand Computer Programming. Why Know Simple Programming?  You can create powerful macros in Access / Excel / Word / ??? to manipulate.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
Questions ? 1. JavaScripts have to be compiled before they are executed. True/False 2. What is the main advantage to interpreted languages? You don’t have.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Lecture 1 Term 2 9/1/12 1. Objects You normally use the “.” Operator to access the value of an object’s properties. The value on the left of the “.” should.
Introduction to JavaScript for Python Programmers
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
JavaScript Objects Objects – JavaScript is an object-based language Has built-in objects we will use – You can create your own objects We concentrating.
Advanced Decisions and Loops Chapter Some Simple Schoolroom Statistics.
CNIT 133 Interactive Web Pags – JavaScript and AJAX Functions.
Using Object-Oriented JavaScript CST 200- JavaScript 4 –
Scripting Languages.
UNIT 3 DYNAMIC WEBSITES WITH CSS AND JAVASCRIPT. OBJECTIVES  CO4 Apply style to a website using CSS.  CO5 Describe the use of scripting when creating.
Sahar Mosleh PageCalifornia State University San Marcos 1 Date, Time, and Timers.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
WEEK 3 AND 4 USING CLIENT-SIDE SCRIPTS TO ENHANCE WEB APPLICATIONS.
1 Overview JavaScript (Recommended Reading : Programming the World Wide Web, 4 th Edition, Robert W. Sebesta, Chapters 4, 5 – available at Steely Library)
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
The Web Wizard’s Guide To JavaScript Chapter 6 Working with Dates and Times.
Working with background, images and date object Basharat Mahmood, Department of Computer Science, CIIT, Islamabad, Pakistan 1.
Chapter 4 JavaScript and Dynamic Web pages. Objectives Static Web pages Dynamic Web pages JavaScript Variables Assignments. JavaScript Functions –(prompt(“”,””)
JavaScript, Fourth Edition
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 JavaScript 1. 2 Java vs. JavaScript Java –Is a high level programming language –Is platform independent –Runs on a standardized virtual machine.
Unit 7 JavaScript Core Objects. Core Objects Core Objects are objects built right into the language. For a complete list of properties and objects available.
JavaScript Functions & Objects. JavaScript Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded URI.
CIS 375—Web App Dev II JavaScript II.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
Using Client-Side Scripts to Enhance Web Applications 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
ALBERT WAVERING BOBBY SENG. Week 5: More JavaScript  Quiz  Announcements/questions.
Lecture 9 The Basics of JavaScript Boriana Koleva Room: C54
Sahar Mosleh California State University San MarcosPage 1 JavaScript An Object-Based Language.
JavaScript Functions & Objects. JavaScript Global Functions FunctionDescription decodeURI()Decodes an encoded URI decodeURIComponent()Decodes an encoded.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
資訊工程學系 3A 彭博涵 2011/03/26 1.  什麼是 JavaScript?  JS Basic  JS Object 2.
Advanced JavaScript Topics – Page 1 of 31CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Advanced JavaScript.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
XP Tutorial 2 New Perspectives on JavaScript, Comprehensive1 Working with Operators and Expressions Creating a New Year’s Day Countdown Clock.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
COP 3813 Intro to Internet Computing Prof. Roy Levow Lecture 4 JavaScript.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
Tutorial 11 1 JavaScript Operators and Expressions.
Chapter 9: Control Statements II CIS 275—Web Application Development for Business I.
JavaScript, Sixth Edition
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled.
JavaScript JavaScript ( Condition and Loops ). Conditional Statements If Statement If...else Statement if (condition) { code to be executed if condition.
Learning Javascript From Mr Saem
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
Chapter 9. An event-driven, object-based programming language that provides various types of functionality to pages. Object based: it is a language that.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
Chapter 22 Selection and Iteration if ( Boolean Expression ) true statement ; else false statement ; OperatorMeaning ! Logical not < Less than
Basic Objects. Math Object  Math.cos( x ), x in radians  Math.sqrt ( x )  Math.pow ( x, y )  Math.ceil( x )  etc.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Introduction to Calculated Columns Variables, Conditionals, and String Manipulation PRESENTER: Cameron Blashka| Informer Implementation Specialist| April.
HTML & teh internets.
Tutorial 12 Working with Arrays, Loops, and Conditional Statements
Java Script.
WEB APPLICATION PROGRAMMING
JavaScript Syntax and Semantics
BY: SITI NURBAYA ISMAIL FACULTY of COMPUTER and MATHEMATICAL SCIENCES
Objects as Variables Featuring the Date Object
The Web Wizard’s Guide To JavaScript
Presentation transcript:

UNIT 5 FUNCTIONS, OBJECTS, CONDITIONS, AND LOOPS

OBJECTIVES  CO6 Create a dynamic website using JavaScript. 2

LEARNING OUTCOMES LO25 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

FUNCTION DEFINITION function Greet() { alert("Greetings."); } 4

FUNCTION WITH PARAMETERS function Greet(who){ alert("Greetings," + who); } 5

FUNCTION IN A Functions function Greet(who){ alert("Greetings," + who); } 6

CALLING THE FUNCTION Function Example Prepare to be greeted twice. Greet("Fred"); Greet("Ethel"); 7

GREET FUNCTION OUTPUT 8

A FUNCTION THAT RETURNS A VALUE Function Example: Average function Average(a,b,c,d){ result = (a + b + c + d)/4; return result; } 9

CALLING THE AVERAGE FUNCTION Function Example: Average The following is the result of the function call. score = Average(3,4,5,6); document.write("The average is:" + score); 10

TYPES OF OBJECTS 11  Built-in objects  DOM objects  Custom objects

CREATING OBJECTS scores = new Array(4); 12

PROPERTIES  A variable stored within the object test = "This is a test."; document.write(test.length);  Each property has a value location.href= " 13

METHOD Function that works with the object's data location.reload(); 14

USING CUSTOM OBJECTS  Name the object  Identify its properties  Create a constructor  Define object methods  Create an instance of an object 15

OBJECT CONSTRUCTOR function Card(name,address,work,home) { this.name = name; this.address = address; this.workphone = work; this.homephone = home; } 16

DEFINING METHODS  Write a function  Add the function definition to the constructor 17

WRITE A FUNCTION function PrintCard() { line1 = "Name: " + this.name + " \n"; line2 = "Address: " + this.address + " \n"; line3 = "Work Phone: " + this.workphone + " \n"; line4 = "Home Phone: " + this.homephone + " \n"; document.write(line1, line2, line3, line4); } 18

ADD THE FUNCTION DEFINITION TO THE CONSTRUCTOR function Card(name,address,work,home) { this.name = name; this.address = address; this.workphone = work; this.homephone = home; this.PrintCard = PrintCard; } 19

CREATING AN OBJECT INSTANCE tom = new Card("Tom Jones", "123 Elm Street", " ", " "); 20

ASSIGN PROPERTY VALUES AFTER OBJECT CREATION holmes = new Card(); holmes.name = "Sherlock Holmes"; holmes.address = "221B Baker Street"; holmes.workphone = " "; holmes.homephone = " "; 21

EXTENDING BUILT-IN OBJECTS Use the prototype keyword to add a property or method to a built-in object. 22

ADDING A METHOD TO THE STRING OBJECT function addhead (level) { html = "h" + level; text = this.toString(); start = " "; stop = " "; 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)); 23

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

ROUNDING EXAMPLE function round(num) { return Math.round(num * 100) / 100; } 25

GENERATING RANDOM NUMBERS function rand(num) { return Math.floor(Math.random() * num) + 1; } 26

WITH KEYWORD with (lastname) { window.alert("length of last name: " + length); capname = toUpperCase(); } 27

DATE FORMATS birthday = new Date(); birthday = new Date ("November 1, :00:00"); birthday = new Date(11,1, 2010); birthday = new Date(11,1,2010, 8, 0, 0); 28

DATE OBJECT SET METHODS  setDate()  setMonth()  setFullYear()  setTime()  setHours()  setMinutes  setSeconds() 29

 getDate()  getMonth()  getFullYear()  getTime()  getHours()  getMinutes()  getSeconds()  getMilliseconds() 30 DATE OBJECT GET METHODS

 getTimeZoneOffset()  toUTCString()  toLocalString()  getUTCDate()  getUTCDay()  getUTCFullYear()  getUTCMonth()  getUTCHours()  getUTCMinutes()  getUTCSeconds()  getUTCMilliseconds()  setUTCDate()  setUTCFullYear()  setUTCMonth()  setUTCHours()  setUTCMinutes()  setUTCSeconds()  setUTCMilliseconds() 31 WORKING WITH TIME ZONES

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

IF STATEMENT if (a == 1) window.alert("Found a 1!"); if (a == 1) { window.alert("Found a 1!"); a = 0; } 33

CONDITIONAL OPERATORS 34 OperatorMeaning ==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

LOGICAL OPERATORS  Or if ((phone == "") || ( == "")) window.alert("Something’s Missing!");  And if ((phone == "") && ( == "")) window.alert("Both are Missing!");  Not if (!(phone == "")) alert("phone is OK"); 35

THE ELSE KEYWORD if (a == 1) { alert("Found a 1!"); a = 0; } else { alert("Incorrect value: " + a); } 36

SHORTHAND CONDITIONAL EXPRESSIONS value = (a == 1) ? 1 : 0; Same as if (a == 1) { value = 1; } else { value = 0; } 37

SHORTHAND EXAMPLE document.write("Found " + counter + ((counter == 1) ? " word." : " words.")); 38

TESTING MULTIPLE CONDITIONS WITH IF AND ELSE if (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

TESTING MULTIPLE CONDITIONS WITH SWITCH switch(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

FOR LOOP for (i=0; i<10; i++) { document.write("This is line " + i + " "); } for (i=0; i<10; i++) document.write("This is line " + i + " "); 41

WHILE LOOP while (total < 10) { n++; total += values[n]; } for (n=0; total < 10; n++) { total += values[n]; } 42

DO...WHILE LOOPS do { n++; total += values[n]; } while (total < 10); 43

INFINITE LOOP  A loop that never reaches the exit condition while (i < 10) { n++; values[n] = 0; }  Can make the browser stop responding 44

BREAK STATEMENT while (true) { n++; if (values[n] == 1) break; } 45

CONTINUE STATEMENT for (i=1; i<21; i++) { if (score[i]==0) continue; document.write( "Student number ",i, " Score: ", score[i], "\n"); } 46

LOOPING THROUGH OBJECT PROPERTIES for (i in navigator) { document.write("property: " + i); document.write(" value: " + navigator[i] + " "); } 47