Java Reference
In-Depth Information
function is executed, the variable userName , used in the body of the function code, will contain
the text "Paul" .
Suppose you wanted to pass two parameters to your function—what would you need to change?
Well, first you'd have to alter the function definition. Imagine that the second parameter will hold
the user's age—you could call it userAge because that makes it pretty clear what the parameter's
data represents. Here is the new code:
function writeUserWelcome(userName, userAge) {
document.write("Welcome to my website" + userName + "<br />");
document.write("Hope you enjoy it<br />");
document.write("Your age is " + userAge);
}
You've added a line to the body of the function that uses the parameter you have added. To call the
function, you'd write the following:
writeUserWelcome("Paul",31);
The second parameter is a number, so there is no need for quotes around it. Here the userName
parameter will be Paul , and the second parameter, userAge , will be 31 .
Fahrenheit to Centigrade Function
trY it out
Let's rewrite the temperature converter page using functions. You can cut and paste most of this code
from ch3 _ example4.html —the parts that have changed have been highlighted. When you've finished,
save it as ch4 _ example1.html .
<!DOCTYPE html>
<html lang="en">
<head>
<title>Chapter 4, Example 1</title>
</head>
<body>
<script>
function convertToCentigrade(degFahren) {
var degCent = 5 / 9 * (degFahren ‐ 32);
return degCent;
}
var degFahren = [212, 32, -459.15];
var degCent = [];
var loopCounter;
for (loopCounter = 0; loopCounter <= 2; loopCounter++) {
degCent[loopCounter] = convertToCentigrade(degFahren[loopCounter]);
}
for (loopCounter = 2; loopCounter >= 0; loopCounter--) {
Search WWH ::




Custom Search