Java Reference
In-Depth Information
4.
Make sure the Require Server Verifi cation check box is unselected, click the Add button, and
then click the Close button.
5.
Click the OK button on the Internet Options dialog to return to the web page, refresh the page
by pressing the F5 key, and the example will now work.
The fi rst line of the script block is a comment, since it starts with two forward slashes ( // ). It contains
the equation for converting Fahrenheit temperatures to centigrade and is in the example code solely for
reference.
// Equation is °C = 5/9 (°F - 32).
Your task is to represent this equation in JavaScript code. You start by declaring your variables, degFahren
and degCent.
var degFahren = prompt(“Enter the degrees in Fahrenheit”,50);
var degCent;
Instead of initializing the degFahren variable to a literal value, you get a value from the user using the
prompt() function. The prompt() function works in a similar way to an alert() function, except that
as well as displaying a message, it also contains a text box in which the user can enter a value. It is this
value that will be stored inside the degFahren variable. The value returned is a text string, but this will
be implicitly converted by JavaScript to a number when you use it as a number, as discussed in the sec-
tion on data type conversion later in this chapter.
You pass two pieces of information to the prompt() function:
The text to be displayed — usually a question that prompts the user for input.
The default value that is contained in the input box when the prompt dialog box fi rst appears.
These two pieces of information must be specifi ed in the given order and separated by a comma. If you
don't want a default value to be contained in the input box when the prompt box opens, use an empty
string (“”) for the second piece of information.
As you can see in the preceding code, the text is “Enter the degrees in Fahrenheit,” and the default
value in the input box is 50 .
Next in the script block comes the equation represented in JavaScript. You store the result of the equa-
tion in the degCent variable. You can see that the JavaScript looks very much like the equation you
have in the comment, except you use degFahren instead of °F, and degCent rather than °C.
degCent = 5/9 * (degFahren - 32);
The calculation of the expression on the right-hand side of the equals sign raises a number of important
points. First, just as in math, the JavaScript equation is read from left to right, at least for the basic math
functions like + , - , and so on. Secondly, as you saw earlier, just as there is precedence in math, there is
in JavaScript.
Starting from the left, fi rst JavaScript works out 5/9 = .5556 (approximately). Then it comes to the mul-
tiplication, but wait . . . the last bit of our equation, degFahren - 32, is in parentheses. This raises the
order of precedence and causes JavaScript to calculate the result of degFahren - 32 before doing the
multiplication. For example, when degFahren is set to 50, (degFahren - 32) = (50 - 32) = 18. Now
JavaScript does the multiplication, .5556 * 18, which is approximately 10.
Search WWH ::




Custom Search