HTML and CSS Reference
In-Depth Information
Nested Conditional Operators
A conditional operator chooses between two possible outcomes. If you want to insert
additional outcomes, you can create a nested conditional operator , in which the out-
come of the second condition is itself a conditional operator. The general syntax is
( condition1 ) ? value1 : ((condition2) ? value2 : value3 );
where value1 is the resulting value when condtion1 is true, value2 is the value when
condition2 is true, and value3 is the value when neither condition is true. For exam-
ple, the nested conditional operator
(x < 10) ? value1 : ((x > 10) ? value2 : value3 );
chooses between two conditions: x < 10 and x > 10.
Note that if x is neither less than nor greater than 10, the only remaining possible out-
come is x = 10 (assuming, of course, that the x variable stores a numeric value), which
would result in value3 being returned by the operator. In general, nested conditional
operators are rarely used in JavaScript programming. Once there are more than two
possible outcomes, it's often easier and better to use conditional statements, which are
covered in the next tutorial.
You've completed your work on the showTime() and showDate() functions. Because
your purpose is to display the current date and time (and the time remaining in the year),
you'll replace the test date of February 24, 2015 with the current date and time. Recall that
a Date object uses the current date and time when you do not specify a date/time value.
To display the current date and time:
1. Return to the clock.htm file in your text editor.
2. Change the command that creates the today variable to the following, as shown in
Figure 11-35:
var today = new Date();
Figure 11-35
changing the today variable to display current date and time
3. Save your changes, and then reload or reopen clock.htm in your Web browser.
The Web page should now display the current date and time as well as the time
remaining in the year.
4. Refresh the Web page, and then verify that the browser updates the date/time
information as well as the amount of time remaining in the year.
 
Search WWH ::




Custom Search