Brain Teaser: Is this a Leap Year?
Brain Teaser: Is this a Leap Year?
A year is a leap year if it’s divisible by 4. But wait, there’s a twist!
If it’s divisible by 100, it’s NOT a leap year unless it’s also divisible
by 400. Tricky, right?
Don’t let the code fool you! Time for a ‘True or False’ quiz!
The Challenge: Leap Year Quiz
Hint: Remember the divisibility rules! 4, 100, and 400 are your keys.
The Code (JavaScript):
function isLeapYear(year) {
if (year % 4 === 0) {
if (year % 100 === 0) {
return year % 400 === 0;
} else {
return true;
}
}
return false;
}
// Examples
console.log(isLeapYear(2024)); // true
console.log(isLeapYear(1900)); // false
console.log(isLeapYear(2000)); // true
console.log(isLeapYear(2023)); // false
console.log(isLeapYear(2028)); // true
Brain check! Did you get them all right?
Now, can you modify the code to handle invalid year inputs (e.g., negative numbers or non-numeric values)?





