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.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
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!