The Information School of the University of Washington Oct 23 fit100-12-functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Introducing JavaScript
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
INFM 603: Information Technology and Organizational Context Jimmy Lin The iSchool University of Maryland Thursday, September 19, 2013 Session 3: JavaScript.
1 COMM 1213 H1 COMP 4923 X1 JavaScript 1 (Readings: Ch. 10, 11 Knuckles)
Chapter 19 Programming Functions. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives Apply JavaScript rules.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Computer Science 103 Chapter 3 Introduction to JavaScript.
1 The Information School of the University of Washington Oct 30fit review Programming Review INFO/CSE 100, Fall 2006 Fluency in Information Technology.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 20 Thinking Big: Functions. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Anatomy of a Function Functions are packages for.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Programming Functions Thinking Big lawrence snyder c h a p t e r 20.
The Information School of the University of Washington Oct 20fit programming1 Programming Basics INFO/CSE 100, Fall 2006 Fluency in Information Technology.
Oct 25 1 The Information School of the University of Washington fit control © 2006 University of Washington Control Flow INFO/CSE 100, Fall 2006.
Introduction to scripting
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Test 2 Part a You have 20 minutes Don’t forget to put your name on the test Closed book No computers Do your own work.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
JavaScript: Functions © by Pearson Education, Inc. All Rights Reserved.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 JavaScript in Context. Server-Side Programming.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Javascript Intro Instructor: Shalen Malabon. Lesson Plan Core Javascript Syntax Types and Objects Functions DOM Event Handling Debugging Javascript Smarter.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Tutorial First lecture 19/2/2016. Javascript is a dynamic computer programming language. It is lightweight and most commonly used as a part.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
© 2010 Robert K. Moniot1 Chapter 6 Introduction to JavaScript.
Barb Ericson Georgia Institute of Technology May 2006
Exploring JavaScript Ch 14
Introduction to Scripting
Functions Chapter 20.
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2005 Fluency in Information Technology
T. Jumana Abu Shmais – AOU - Riyadh
INFO/CSE 100, Spring 2006 Fluency in Information Technology
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Tutorial 10: Programming with javascript
INFO/CSE 100, Spring 2005 Fluency in Information Technology
JavaScript: Introduction to Scripting
Javascript Chapter 19 and 20 5/3/2019.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Programming Basics Review
Presentation transcript:

The Information School of the University of Washington Oct 23 fit functions 1 Functions / If Else INFO/CSE 100, Fall 2006 Fluency in Information Technology

The Information School of the University of Washington Oct 23 fit functions 2 Readings and References Reading »Fluency with Information Technology Chapter 20, Abstraction and Functions Other References »W3Schools JavaScript tutorial »W3Schools JavaScript HTML DOM Objects »Mozilla Browser

The Information School of the University of Washington Oct 23 fit functions 3 Functions A function is a way to bundle a set of instructions and give them a name so that you can reuse them easily Functions have a specific layout »  the function name is an identifier »  list of input variables for the function »  the statements do the work function ( ) { }

The Information School of the University of Washington Oct 23 fit functions 4 Example Function Write a simple function to compute the Body Mass Index when the inputs are in English units (ie, US units) function ( ) { } // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function bmiE(weightLBS, heightIN) { var heightFt = heightIn / 12; // convert to feet return 4.89 * weightLBS / (heightFt * heightFt); } template example

The Information School of the University of Washington Oct 23 fit functions 5 Develop the function // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function name(parameter list) { statements } First, make sure you understand what you want the function to do and how it will accomplish the task.

The Information School of the University of Washington Oct 23 fit functions 6 Pick a name for the function Function names are identifiers »start with a letter »should have a fairly obvious meaning »should not be one of the Javascript reserve words // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function bmiE(parameter list) { statements }

The Information School of the University of Washington Oct 23 fit functions 7 Pick the parameters Parameter names are also identifiers »these are the variable names that your function will use when it is performing its calculations »should have a fairly obvious meaning // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function bmiE(weightLBS, heightIN) { statements; }

The Information School of the University of Washington Oct 23 fit functions 8 Functions without Parameters! Function do not have to have parameters »But we still need to include the parentheses // Print out Greeting // Typical Greeting is "Hello World" function giveGreeting() { document.write("Hello World!"); }

The Information School of the University of Washington Oct 23 fit functions 9 Write the function body The function body includes whichever statements are required to implement the desired capability. // Calculate Body Mass Index in English units // weight in pounds // height in inches // returns body mass index function bmiE(weightLBS, heightIN) { var heightFt = heightIn / 12; // convert to feet return 4.89 * weightLBS / (heightFt * heightFt); }

The Information School of the University of Washington Oct 23 fit functions 10 A Simple Testing Template <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " Body Mass Index // Figure Body Mass Index in English units function bmiE( weightLBS, heightIn ) { var heightFt = heightIn / 12; // Change to feet return 4.89 * weightLBS / (heightFt * heightFt); } This page provides a simple body mass index calculator. Normal weight corresponds to a BMI of document.writeln(" bmiE(100,72): "+bmiE(100,72)); document.writeln(" bmiE(150,72): "+bmiE(150,72)); document.writeln(" bmiE(175,72): "+bmiE(175,72)); document.writeln(" bmiE(200,72): "+bmiE(200,72)); The new function Test statements

The Information School of the University of Washington Oct 23 fit functions 11 Try the function and see how it works

The Information School of the University of Washington Oct 23 fit functions 12 Global or Local?!? Scope of a variable describes where and when it can be referenced »Local variables are only known inside of a function (curly braces) »Global variables are know by all the Javascript inside of pairs // Calculate Percentage of Study Hours/Week // time in hours // returns hours var days = 7; function calculateStudyHrs(time) { var totalHrs = 24 * days; return time/totalHrs; }

The Information School of the University of Washington Oct 23 fit functions 13 The if / else statement The if statement is a conditional statement »a conditional expression is evaluated as being true or false the expression is a boolean expression (ie, returns true or false ) »if the condition is true, then one set of statements is executed »if the statement is false, then a different set of statements is executed if ( ) { } else { }

The Information School of the University of Washington Oct 23 fit functions 14 Examples if (count == 0) { ready = false; } else { ready = true; count = count-1; } What is the conditional expression? What statements are part of the true block? Which statements are part of the false block? What happens when count is 21? 0? -1? if (pageCount >= 100) { alert("This may take a few minutes."); } Which statements are part of the false block? What happens when pageCount is 21? 100? 200? What is the conditional expression? What statements are part of the true block?

The Information School of the University of Washington Oct 23 fit functions 15 Brian’s Black Cup Extended A Walk through…

The Information School of the University of Washington Oct 23 fit functions 16 Comments on Debugging Debugging JavaScript can be hard »The browsers all implement things a little differently, particularly old browsers upgrade if you are using something old!

The Information School of the University of Washington Oct 23 fit functions 17 Use the W3Schools TryIt Editor

The Information School of the University of Washington Oct 23 fit functions 18 Display results using alert(...) Use the alert("This is a message") function

The Information School of the University of Washington Oct 23 fit functions 19 Use an editor that helps you The jEdit editor helps you by doing syntax highlighting.

The Information School of the University of Washington Oct 23 fit functions 20 Display results using writeln(...) document.writeln("someVariable: "+someVariable);

The Information School of the University of Washington Oct 23 fit functions 21 Use a browser that helps you All browsers try to be forgiving of errors, which means that they generally don't produce a lot of error messages »use a browser that helps you debug like Mozilla

The Information School of the University of Washington Oct 23 fit functions 22 enable Mozilla JavaScript Console

The Mozilla JavaScript console helps you by showing good error messages.

The Information School of the University of Washington Oct 23 fit functions 24 Homework 3 Found on Website Due: Oct 30 in class