Java Reference
In-Depth Information
}
if (degCent == 100)
document.write(“That's the boiling point of water”);
</script>
</body>
</html>
2.
Load the page into your browser and enter 32 into the prompt box for the Fahrenheit value to
be converted. With a value of 32 , neither of the if statement's conditions will be true, so the
only line written in the page will be that shown in Figure 3-3.
32° Fahrenheit is 0° centigrade
Figure 3-3
3.
Now reload the page and enter 31 for the Fahrenheit value. This time you'll see two lines in the
page, as shown in Figure 3-4.
31° Fahrenheit is -0.5555555555555556° centigrade
That's below the freezing point of water
Figure 3-4
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.
212° Fahrenheit is 100° centigrade
That's the boiling point of water
Figure 3-5
The fi rst part of the script block in this page is taken from the example ch2_examp4.htm 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 explicitly convert to a numeric value using the Number() function. The variable degCent is
then set to the result of the calculation 5/9 * (degFahren - 32) , which is the Fahrenheit-to-centigrade
conversion calculation.
var degFahren = Number(prompt(“Enter the degrees Fahrenheit”,32));
var degCent;
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 />”);
Search WWH ::




Custom Search