Finding next n leap years in JavaScript

We are required to write a function that takes a positive integer n and returns an array of next n leap years. We will break this problem into three parts:

Part 1: Finding Current Year via JavaScript

The code to find current year via JavaScript will be:

// getting the current year from a new instance of Date object
const year = new Date().getFullYear();
console.log("Current year:", year);
Current year: 2024

Part 2: Checking for Leap Year

We will now write a function isLeap() that takes in a number and returns a boolean based on the number being a leap year or not.

A year is considered as a leap year if at least one of these two conditions are met:

  • It is multiple of 400.
  • It is multiple of 4 and not multiple of 100.

Keeping these things in mind let's write the function isLeap():

// function to check for a leap year
const isLeap = year => {
    return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
};

// Test the function
console.log(isLeap(2024)); // true
console.log(isLeap(2025)); // false
console.log(isLeap(2000)); // true (divisible by 400)
console.log(isLeap(1900)); // false (divisible by 100 but not 400)
true
false
true
false

Part 3: Finding Next n Leap Years

Now we'll combine everything to create a function that finds the next n leap years starting from the year after the current year:

// function to check for a leap year
const isLeap = year => {
    return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
};

const nextNLeap = n => {
    const arr = [];
    let year = new Date().getFullYear() + 1;
    while(arr.length < n){
        if(isLeap(year)){
            arr.push(year);
        }
        year++;
    }
    return arr;
};

console.log("Next 5 leap years:", nextNLeap(5));
console.log("Next 10 leap years:", nextNLeap(10));
Next 5 leap years: [ 2024, 2028, 2032, 2036, 2040 ]
Next 10 leap years: [
  2024, 2028, 2032, 2036,
  2040, 2044, 2048, 2052,
  2056, 2060
]

How It Works

The algorithm works by:

  • Starting from the year after the current year
  • Checking each year to see if it's a leap year using our isLeap() function
  • Adding leap years to the result array until we have n leap years
  • Incrementing the year counter for each iteration

Complete Example

// Complete solution
const isLeap = year => {
    return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
};

const nextNLeap = n => {
    const arr = [];
    let year = new Date().getFullYear() + 1;
    while(arr.length < n){
        if(isLeap(year)){
            arr.push(year);
        }
        year++;
    }
    return arr;
};

// Test with different values
console.log("Next 3 leap years:", nextNLeap(3));
console.log("Next 7 leap years:", nextNLeap(7));
Next 3 leap years: [ 2024, 2028, 2032 ]
Next 7 leap years: [
  2024, 2028, 2032,
  2036, 2040, 2044,
  2048
]

Conclusion

This solution efficiently finds the next n leap years by combining date manipulation, leap year logic, and array building. The key insight is using the correct leap year formula and systematically checking each year until we find n leap years.

Updated on: 2026-03-15T23:18:59+05:30

636 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements