Title: JavaScript Lecture 4
1JavaScript Lecture 4
- Assignment
- Review (examples for- if, prompts)
- Functions
- Note The following slides give the code and the
output of the examples covered in class.
2lthtmlgt ltheadgt lttitlegtIFlt/titlegt lt/headgt ltbody
gt ltscript type "text/javascript"gt var
watertemp0 var waterstate"liquid"
var description"water" if (watertemp lt
0) waterstate "frozen"
description "ice" document
.write("ltbrgtltbrgt") document.write("Watertemp
"watertemp"ltbrgt") document.write
("Waterstate "waterstate"ltbrgt")
document.write ("Description
"description) lt/scriptgt lt/bodygt lt/htmlgt
3OUTPUT
- Watertemp 0
- Waterstate frozen
- Description ice
- Note change the value for watertemp and check
the output
4- lthtmlgt
- ltheadgt lttitlegtSIMPLE IFlt/titlegtlt/headgt
- ltbodygt
- ltscript type "text/javascript"gt
- var a true
-
- if (atrue)
-
- document.write("true")
-
- if (afalse)
- document.write("false")
- lt/scriptgt
- lt/bodygt
- lt/htmlgt
5OUTPUT
6- lthtmlgt
- ltheadgt lttitlegtSIMPLE IFlt/titlegtlt/headgt
- ltbodygt
- ltscript type "text/javascript"gt
- var a true
-
- if (a)
-
- document.write("true")
-
- if (!a)
- document.write("false")
- lt/scriptgt
- lt/bodygt
- lt/htmlgt
7OUTPUT
- true
- Note The last two programs are equivalent and
give the same output.
8- lthtmlgt
- ltheadgt lttitlegtSIMPLE IFlt/titlegtlt/headgt
- ltbodygt
- ltscript type "text/javascript"gt
- var a 3
- if (a)
-
- document.write("true")
- if (!a)
- document.write("false")
- lt/scriptgt
- lt/bodygt
- lt/htmlgt
9OUTPUT
- true
- Note Change the value of a to 0 and see what
happens.
10- lthtmlgt
- ltheadgt
- lttitlegtCONFIRM IFlt/titlegt
- lt/headgt
- ltbodygt
- ltscript type "text/javascript"gt
-
- var a
- a confirm("Do you want to continue?")
- if (a)
- alert("You chose to continue")
- else
- alert("You chose to NOT continue")
-
11Output Confirm box
12Output after clicking on OK
13Output after clicking on Cancel
14- lthtmlgt
- ltheadgt lttitlegtIF ELSElt/titlegtlt/headgt
- ltbodygt
- ltscript type"text/javascript"gt
- var watertemp 5
- var waterstate
- var description
- if (watertemp lt0)
-
- waterstate "frozen"
- description "ice"
-
- else
-
- waterstate "liquid"
- description "water"
15Output
- Watertemp 5
- Waterstate liquid
- Description water
16Function Packages for algorithms
- Name
- Parameters
- Definition
- Also known as,
- procedures, subroutine, method etc
17- function name (parameter list)
-
- statement list
18- function print ()
-
- document.write(hello)