Creating Arrays

In the above version of the XmasDays function, you were limited to working with a single month, October. However, your clients want this function to work for any month. For this to happen, you need a variable that can store multiple values. This variable will need to store the number of days in each of the 12 months of the year. This type of variable is called an array. You will now create an array named "MonthCount." You will use values from the array in the XmasDays function to calculate the days remaining until Christmas.

An array is an ordered collection of values referenced by a single variable name. The syntax for creating an array variable is:

var Month = new Array();

Month[12]="December";

Return to your text editor. Below the function statement "function XmasDays(Month, Day, Year) {," insert the following lines (indented to make your program easier to read):

var MonthCount=new Array();

MonthCount[1]=31;

MonthCount[2]=28;

MonthCount[3]=31;

MonthCount[4]=30;

MonthCount[5]=31;

MonthCount[6]=30;

MonthCount[7]=31;

MonthCount[8]=31;

MonthCount[9]=30;

MonthCount[10]=31;

MonthCount[11]=30;

Edit the line that reads "var DayCount=(25-Day) + 31 + 30" to read:

var DayCount=(25-Day) + MonthCount[10] + MonthCount[11];

 

Just Plain Stupid