Java Reference
In-Depth Information
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
param-
eter will be
Paul
, and the second parameter,
userAge
, will be
31
.
Try It Out Fahrenheit to Centigrade Function
Let's rewrite the temperature converter page using functions. You can cut and paste most of this code
from
ch3_examp4.htm
— the parts that have changed have been highlighted. When you've fi nished,
save it as
ch3_examp5.htm
.
<html>
<body>
<script language=”JavaScript” type=”text/javascript”>
function convertToCentigrade(degFahren)
{
var degCent;
degCent = 5/9 * (degFahren - 32);
return degCent;
}
var degFahren = new Array(212, 32, -459.15);
var degCent = new Array();
var loopCounter;
for (loopCounter = 0; loopCounter <= 2; loopCounter++)
{
degCent[loopCounter] = convertToCentigrade(degFahren[loopCounter]);
}
for (loopCounter = 2; loopCounter >= 0; loopCounter--)
{
document.write(“Value “ + loopCounter + “ was “ + degFahren[loopCounter] +
“ degrees Fahrenheit”);
document.write(“ which is “ + degCent[loopCounter] +
“ degrees centigrade<br />”);
}
</script>
</body>
</html>