S
Sean
Hi
As part of my JavaScript course I have been asked to do a switch
statement which displays the days of the month when the user enters a
number, easy enough.
but for February I need to display two conditions, one if it is not a
leap year and another one if it is a leap year
I have figured out how to get the year, and have been instructed to
display one condition if the number divides evenly by 4 (leap year)
and another if it doesn't.
do I use a comparison operator or something to do this, I have no
idea?
also once I can do this and have the if...else statement working
correctly, how can I include this in the switch statement?
Thanks
var today = new Date();
var year = today.getYear();
alert(year) // displays 2005
if (year / 4 == 501.25) // is 501.25
alert("There are 28 days because it is NOT a leap year") //
doesn't divide evenly by 4, an integer
else
alert("There are 29 days because it is a leap year") // does
not divide evenly by 4, a floating point number
var names, months;
months = parseInt(prompt("Please enter the number of the month 1
through 12", ""));
switch (months)
{
case 1 : alert("January has 31 days in it");
break;
case 2 : alert("February has 28 days in it");
break;
case 3 : alert("March has 31 days in it");
break;
case 4 : alert("April has 30 days in it");
break;
case 5 : alert("May has 31 days in it");
break;
case 6 : alert("June has 30 days in it");
break;
case 7 : alert("July has 31 days in it");
break;
case 8 : alert("August has 31 days in it");
break;
case 9 : alert("September has 30 days in it");
break;
case 10 : alert("October has 31 days in it");
break;
case 11 : alert("November has 30 days in it");
break;
case 12 : alert("December has 31 days in it");
break;
default : alert("You entered some other information, please
try again!");
}
As part of my JavaScript course I have been asked to do a switch
statement which displays the days of the month when the user enters a
number, easy enough.
but for February I need to display two conditions, one if it is not a
leap year and another one if it is a leap year
I have figured out how to get the year, and have been instructed to
display one condition if the number divides evenly by 4 (leap year)
and another if it doesn't.
do I use a comparison operator or something to do this, I have no
idea?
also once I can do this and have the if...else statement working
correctly, how can I include this in the switch statement?
Thanks
var today = new Date();
var year = today.getYear();
alert(year) // displays 2005
if (year / 4 == 501.25) // is 501.25
alert("There are 28 days because it is NOT a leap year") //
doesn't divide evenly by 4, an integer
else
alert("There are 29 days because it is a leap year") // does
not divide evenly by 4, a floating point number
var names, months;
months = parseInt(prompt("Please enter the number of the month 1
through 12", ""));
switch (months)
{
case 1 : alert("January has 31 days in it");
break;
case 2 : alert("February has 28 days in it");
break;
case 3 : alert("March has 31 days in it");
break;
case 4 : alert("April has 30 days in it");
break;
case 5 : alert("May has 31 days in it");
break;
case 6 : alert("June has 30 days in it");
break;
case 7 : alert("July has 31 days in it");
break;
case 8 : alert("August has 31 days in it");
break;
case 9 : alert("September has 30 days in it");
break;
case 10 : alert("October has 31 days in it");
break;
case 11 : alert("November has 30 days in it");
break;
case 12 : alert("December has 31 days in it");
break;
default : alert("You entered some other information, please
try again!");
}