HTML and CSS Reference
In-Depth Information
To translate these rules into our calendar app, we need to nest one if statement
inside of another. The general structure of this nested if statement is as follows:
if (thisYear % 4 == 0) {
further test for century years
}
The nested if statement needs to add two more conditions: (1) the year is not divisible
by 100, and (2) the year is divisible by 400. The expressions for these two conditions are
as follows:
thisYear % 100 != 0
thisYear % 400 == 0
If either of those two conditions is true for a year evenly divisible by 4, then the year is a
leap year. Note that you will use the not equal to operator ( != ) to test for an inequal-
ity in the first expression. You'll then combine these two expressions into a single expres-
sion using the or operator ( || ), as follows:
(thisYear % 100 != 0) || (thisYear % 400 == 0)
Finally, you'll nest this conditional expression as follows:
if (thisYear % 4 == 0) {
if ((thisYear % 100 != 0) || (thisYear % 400 == 0)) {
dayCount[1] = 29;
}
}
Under this set of nested if statements, the number of days in February is 29 only if the
thisYear variable is divisible by 4, and then only if it also is divisible by 400 or not divis-
ible by 100. Take some time to compare this set of nested if statements with the chart
shown in Figure 12-21 to confirm that it satisfies all possible conditions for leap years.
After incorporating this set of nested if statements, the daysInMonth() function returns
the number of days for any month in any given year.
To complete the daysInMonth() function:
1. Within the if statement you entered in the last set of steps, delete the statement
dayCount[1] = 29 .
2. Replace the statement you just deleted with the following nested if statement:
if ((thisYear % 100 != 0) || (thisYear % 400 == 0)) {
dayCount[1] = 29;
}
Figure 12-22 highlights the newly inserted nested if statement.
Search WWH ::




Custom Search