Functions / If Else - PowerPoint PPT Presentation

About This Presentation
Title:

Functions / If Else

Description:

title Body Mass Index /title script type ... p This page provides a simple body mass index calculator. Normal weight corresponds to a BMI of 18.5-24.9 /p ... – PowerPoint PPT presentation

Number of Views:129
Avg rating:3.0/5.0
Slides: 25
Provided by: douglaswjo
Category:

less

Transcript and Presenter's Notes

Title: Functions / If Else


1
Functions / If Else
  • INFO/CSE 100, Fall 2006
  • Fluency in Information Technology

http//courses.washington.edu/info100/
2
Readings and References
  • Reading
  • Fluency with Information Technology
  • Chapter 20, Abstraction and Functions
  • Other References
  • W3Schools JavaScript tutorial
  • http//www.w3schools.com/js/default.asp
  • W3Schools JavaScript HTML DOM Objects
  • http//www.w3schools.com/js/js_obj_htmldom.asp
  • Mozilla Browser
  • http//www.mozilla.org/

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
  • ltnamegt ? the function name is an identifier
  • ltparameter listgt ? list of input variables for
    the function
  • ltstatementsgt ? the statements do the work

function ltnamegt ( ltparameter listgt )
ltstatementsgt
4
Example Function
function ltnamegt ( ltparameter listgt )
ltstatementsgt
template
  • Write a simple function to compute the Body Mass
    Index when the inputs are in English units (ie,
    US units)

// 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)
example
5
Develop the function
First, make sure you understand what you want the
function to do and how it will accomplish the
task.
// Calculate Body Mass Index in English units //
weight in pounds // height in inches // returns
body mass index function name(parameter list)
statements
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
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
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!")
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)
10
A Simple Testing Template
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http//www.w3.org/TR/html4/l
oose.dtd"gt lthtmlgt ltheadgt lttitlegtBody Mass
Indexlt/titlegt ltscript type"text/javascript"gt //
Figure Body Mass Index in English units function
bmiE( weightLBS, heightIn ) var heightFt
heightIn / 12 // Change to feet return 4.89
weightLBS / (heightFt heightFt) lt/scriptgt lt/h
eadgt ltbodygt ltpgtThis page provides a simple body
mass index calculator. Normal weight corresponds
to a BMI of 18.5-24.9lt/pgt ltscript
type"text/javascript"gt document.writeln("ltbrgtbmiE
(100,72) "bmiE(100,72)) document.writeln("ltbrgtb
miE(150,72) "bmiE(150,72)) document.writeln("ltb
rgtbmiE(175,72) "bmiE(175,72)) document.writeln(
"ltbrgtbmiE(200,72) "bmiE(200,72)) lt/scriptgt lt/bo
dygt lt/htmlgt
The new function
Test statements
11
Try the function and see how it works
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 ltscriptgt lt/scriptgt 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
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 (ltboolean expressiongt) ltstatementsgt else
ltstatementsgt
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 gt 100) alert("This may take a
few minutes.")
What is the conditional expression? What
statements are part of the true block?
Which statements are part of the false
block? What happens when pageCount is 21? 100?
200?
15
Brians Black Cup Extended
  • A Walk through

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!

17
Use the W3Schools TryIt Editor
18
Display results using alert(...)
  • Use the alert("This is a message") function

19
Use an editor that helps you
  • The jEdit editor helps you by doing syntax
    highlighting.

20
Display results using writeln(...)
ltbodygt ltscript type"text/javascript"gt document.wr
iteln("someVariable "someVariable) lt/scriptgt lt/
bodygt
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

22
enable Mozilla JavaScript Console
23
The Mozilla JavaScript console helps you by
showing good error messages.
24
Homework 3
Found on Website Due Oct 30 in class
Write a Comment
User Comments (0)
About PowerShow.com