LearnFrenzy
Sign in Take a free test

JavaScript Basics · JavaScript Basics Coding Problems · Question 9

Check leap years in JavaScript Solution, output & explanation

  • Program
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

Print whether each of the years 1900, 2000, 2024 and 2026 is a leap year.

Solution
main.js
const years = [1900, 2000, 2024, 2026];

for (const year of years) {
  // Divisible by 4, except centuries – unless the century is divisible by 400
  const leap = (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
  console.log(`${year}: ${leap ? "leap year" : "not a leap year"}`);
}

Output Verified by running it on Node.js

1900: not a leap year
2000: leap year
2024: leap year
2026: not a leap year

1900 is divisible by 100 but not by 400, so it is not a leap year; 2000 is divisible by 400. A common wrong answer checks only year % 4.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links