HTML and CSS Reference

In-Depth Information

this value down to the next lowest integer using the
Math.floor()
method, as shown in

the following expression:

Math.floor(hours)

The value of the
hours
variable will then be equal to 9 for the current date. You'll add

these two expressions to the JavaScript code in the
clock.htm
file.

To calculate the hours left in the day:

◗

1.
Return to the
clock.htm
file and the NYClock() function in your text editor.

◗

2.
Below the statement that sets the value of the
daysLeft
field, insert the following

two lines:

// calculate the hours left in the current day

var hours = (days - Math.floor(days))*24;

◗

3.
Change the statement that sets the value of the
hrsLeft
field to the following, as

shown in Figure 11-24:

// display hours rounded to the next lowest integer

document.clockform.hrsLeft.value = Math.floor(hours) + “ hour(s)”;

Figure 11-24

hours variable added to the nYclock() function

calculates the hours

left in the current day

rounds the hours value

down to the nearest integer

◗

4.
Save your changes to the file, and then reopen or refresh
clock.htm
in your Web

browser. The
hrsLeft
field displays the hours left in the current day as an integer.

See Figure 11-25.