Instructor: Amadou O. Wane
Last update: 09/05/01
Lecture3
//OR OPERATOR
var city = "Paris";
var state = "FL";
var country = "Nowhere";
if ( city == "Tampa" || state == "FL")
{ country = "USA";
document.write("<br>You live in the " + country);
}
//------------------------------------------------------------------
//TYPEOF OPERATOR
//var my_typeof = typeof (city>state);
var my_typeof = typeof 8;
document.write("<br>my_typeof is " + "<b>" + my_typeof + "</b>");
//------------------------------------------------------------------
//PROMPT
//var answer = prompt("Enter a number?", "");
//answer++;
//document.write("You said your name is " + answer);
//------------------------------------------------------------------
var city1 = "Tampa";
var city2 = "Orlando";
var city3 = "Miami";
answer = prompt("Enter city of residence:", "");
if (answer == city1 || answer == city2 || answer == city3)
{ document.write("<br> You live in Florida");
}
else
{document.write("<br>I know you don't live in Tampa or Orlando or Miami");
}
//------------------------------------------------------------------
Functions
What if you wanted to use our grading script for more than one student?
You would have to repeat it for each student. This could get quite lengthy
if there were more than a very few students. You can however use a
function to contain the script and call it every time your need it. Here
is the syntax for a function.
function name (parameters){
statements
}
The function
keyword identifies this as a function. The parameters
in the parenthesis ( ) provide a means of
passing values to the function. There can be as many parameters
separated by commas as you need. It is perfectly ok to have a function
with no parameters. These parameters are variables which are used
by the JavaScript statements inside
the curly braces { }. The var keyword is not
needed to declare the parameters in a function as variables because they
are automatically declared and initialized when the function is called.
The function can return a value by using the return
keyword.
You should put your functions in the
HEAD section of your document. Functions will work if you put them in the
BODY section. However, a function must be loaded prior to the statement
that calls it. Putting all functions in the HEAD section is the way to
insure this.
The best way to demonstrate a
function is with an example. Suppose we want to make a function that we
can call anytime to add two numbers together. Here is one way of writing
the function.
function myAdder (num1, num2){
var total = num1 + num2
document.write(total)
}
You would place this function inside
some <SCRIPT> tags in the head section. You then can call the
function from anywhere in your document. In this example, lets call it
from within some <SCRIPT> tags in the body of document like this.
myAdder(23, 56)
//EXAMPLE
function myAddition (numb1,numb2)
{ var my_sum = numb1 + numb2;
return my_sum;
}
document.write("<br>The answer is " + myAddition(1,2));
document.write("<br>The answer is " + myAddition(10,20));
my_sum2 = myAddition(10,40) + 2;
document.write("<br>The answer of my_sum2 is " + my_sum2);
|