Skip to content

Instantly share code, notes, and snippets.

@ThomasPCG
Last active June 7, 2018 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasPCG/4dbdf8a727d61bb0ba45ee24f8649a9e to your computer and use it in GitHub Desktop.
Save ThomasPCG/4dbdf8a727d61bb0ba45ee24f8649a9e to your computer and use it in GitHub Desktop.
Checking and getting leap years in pure Node.js
//Check if parameter yr is a leap year.
var isLeapYear = function(yr) {
return (((yr % 4 === 0) && (yr % 100 !== 0)) || (yr % 400 === 0));
};
//Get leap year
// Paramaters:
// yr - starting year
// iteration - number of years to print out
// count - internal variable for tracking
var getLeapYear = function(yr, iteration, count = 0) {
if (isLeapYear(yr)) {
count++;
console.log(yr);
}
if (count < iteration) {
return getLeapYear(yr+1, iteration, count);
}
return true;
};
//Demo
var currentYear = new Date().getFullYear();
getLeapYear(currentYear,20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment