Last active
June 7, 2018 09:27
-
-
Save ThomasPCG/4dbdf8a727d61bb0ba45ee24f8649a9e to your computer and use it in GitHub Desktop.
Checking and getting leap years in pure Node.js
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
//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