HTML and CSS Reference
In-Depth Information
Working with Conditional Statements
Your next task in your calendar app is to create a program loop that writes the days of the
month, entered within different table cells arranged in separate table rows. The process
should end when the last day of the month is reached. Because months have different
numbers of days, you first need to create a function named daysInMonth() that deter-
mines the number of days in a given month.
Like the writeCalTitle() function you created earlier, the daysInMonth() function will
have a single parameter, calendarDay , containing a date object on which your calen-
dar will be based. The function also will store the year value and month value in the
variables thisYear and thisMonth , respectively, and will contain the following array that
stores the number of days in each month:
var dayCount = [31,28,31,30,31,30,31,31,30,31,30,31];
This array is an example of a parallel array because each entry in the array matches—or is
parallel to—an entry in the monthName array you created in the first session. To return the days
of the month from the calendar date, the function will use the value of the thisMonth variable
to reference the corresponding day value in the dayCount array with the following expression:
So, for instance, given the date July 6, 2015, the function would return the value 31.
You'll add the daysInMonth() function now.
To start creating the daysInMonth() function:
1. Return to the calendar.js file in your text editor.
2. At the bottom of the file, insert the following code, as shown in Figure 12-19:
function daysInMonth(calendarDay) {
// Array of days in each month
var dayCount = [31,28,31,30,31,30,31,31,30,31,30,31];
// Extract the four digit year value from calendarDay
var thisYear = calendarDay.getFullYear();
// Extract the month value from calendarDay
var thisMonth = calendarDay.getMonth();
// Return the number of days for the current month
return dayCount[thisMonth];
Figure 12-19
inserting the daysinMonth() function
Search WWH ::

Custom Search