Using the If...Else Statement

The If statement you just entered runs a set of commands if the condition is true, but does nothing if the condition is false. Sometimes you want the If statement to run one set of commands if the condition is true and another set of commands if the condition is false. An If...Else statement allows you to do this. The syntax for this statement is:

if(condition){

JavaScript Commands if true

} else {

JavaScript Commands if false

}

where condition is an expression that is either true of false, and one set of commands is run if the expression is true, and another is run if the expression is false.

You will use this command to display a message from December 25th or later (through December 31st), otherwise it will display the number of days until Christmas as calculated by the XmasDays function.

You will distinguish between the two situations by creating an If...Else statement that looks at the value returned by the XmasDays function. If that value is positive, then the current date is before December 25th, and the page should display the number of days left in the holiday season. On the other hand, if the value is 0 or negative, then the current date is December 25th of later in the year, and a holiday message should be generated. The code to perform this is as follows:

if(DaysLeft > 0) {

document.write("Only " + DaysLeft + " days until Christmas";

} else {

document.write("Have a Wild and Wacky Holiday from Just Plain Stupid!"

}

Replace the line "document.write("Only " + DaysLeft + " days until Christmas");" with:

//Display either the number of days until Christmas, or a holiday greeting

if(DaysLeft > 0) {

document.write("Only " + DaysLeft + " days until Christmas");

} else {

document.write("Have a Wild and Wacky Holiday from Just Plain Stupid!");

}

Save your changes and then reopen it in your Web browser. The page should display the "only so many days until Christmas.

******Change your date to December 26 and resave it. Display it in your browser. It should now show your holiday greeting!!!!!!!

The last thing you want to do is change the line "var Today=new Date("December 26, 2001"); to:

var Today = new Date();

This will now display todays date and will change the date each day.

Congratulations on completing your first Web page!!! You are not a Web Master yet, but you are well on your way!!!!!

Just Plain Stupid