Java Reference
In-Depth Information
4.
Finally, reload the page again, but this time, enter 212 in the prompt box. The two lines shown in
Figure 3-5 will appear in the page.
figure 3-5  
The first part of the script block in this page is similar to the example ch2 _ example4.html in
Chapter 2. You declare two variables, degFahren and degCent . The variable degFahren is given
an initial value obtained from the user with the prompt() function. Note the prompt() function
returns a string value, which you then convert to a numeric value using the parseInt() function. The
variable degCent is initialized to the result of the calculation 5/9 * (degFahren ‐ 32) , which is the
Fahrenheit‐to‐centigrade conversion calculation:
var degFahren = parseInt(prompt("Enter the degrees Fahrenheit", 32), 10);
var degCent = 5/9 * (degFahren - 32);
Then you write the result of your calculation to the page:
document.write(degFahren + "\xB0 Fahrenheit is " + degCent +
"\xB0 centigrade<br />");
Now comes the new code; the first of two if statements:
if (degCent < 0) {
document.write("That's below the freezing point of water");
}
This if statement has the condition that asks, “Is the value of the variable degCent less than zero?” If
the answer is yes ( true ), the code inside the curly braces executes. In this case, you write a sentence to
the page using document.write() . If the answer is no ( false ), the processing moves on to the next line
after the closing brace. Also worth noting is the fact that the code inside the if statement's opening
brace is indented. This is not necessary, but it is a good practice to get into because it makes your code
much easier to read.
Search WWH ::




Custom Search