Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding question

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How would you prompt a user to input a year and base on the year determine if it is a leap year.
 
You can use the standard prompt to get input:

var yr=prompt("Enter a year","");

The rule for leap years is it's a leap year if the year is both evenly divisible by 4 and not evenly divisible by 100. However, all years evenly divisible by 400 are also leap years. So, a simple function to tell you if it's a leap year or not could be done like this:

function isLeapYear(yr)
{
if (((yr%4==0) && (yr%100!=0)) || yr%400==0) {
return true;
} else {
return false;
}
}

You should also insert some sanity checks before passing the year to the function (make sure the user entered a value, and that the value is a positive number etc.).

HTH,

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top