Last active
January 22, 2020 11:55
-
-
Save 19Site/c915bda58240e5768e405f30849bfbee to your computer and use it in GitHub Desktop.
Check year is leap year
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function isLeapYear(year) { | |
// result | |
var result = undefined; | |
// set as today | |
if (typeof year === 'undefined') { | |
year = new Date().getYear(); | |
} | |
// is date object | |
else if (typeof year === 'object' && year instanceof Date) { | |
// get year | |
year = year.getYear(); | |
} | |
// year is number | |
if (typeof year === 'number' && year !== NaN) { | |
// assign result | |
result = ((year % 4) === 0 && (year % 100) !== 0) || (year % 400) === 0; | |
} | |
// valid result | |
if (typeof result === 'number') { | |
// return result | |
return result; | |
} | |
// invalid result | |
else { | |
// invalid input value | |
throw new Error('invalid input value'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment