Build a Leap Year Calculator - Build a Leap Year Calculator

Tell us what’s happening:

I don’t understand what is wrong here, I checked code many times but it doesn’t helps. Please help me

Your code so far

function isLeapYear(year) {
  if((year % 4) === 0 && (year % 400) === 0) {
    return `${year} is a leap year.`
  } else if((year % 4) === 0 && (year%100) === 0) {
    return `${year} is not a leap year.`
  }
}

let year = 2024;
let result = isLeapYear(year);
console.log(result);

year = 2000;
result = isLeapYear(year);
console.log(result);

year = 1900;
result = isLeapYear(year);
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Build a Leap Year Calculator - Build a Leap Year Calculator

You’re almost there but your logic doesn’t account for any case where the year is divisible by 4 but not divisible by either 100 or 400 (e.g. 2024, which is one of the cases which you’re failing, as your function returns undefined in this case).

you are also not accounting for years that are not divisible by 4. like 1997

1 Like

how can I think like you that how many cases I need to create to write a robust condition ? Is there any mathematical formula ?

Yes, doing it now. Also I want to ask if there is any mathematical formula to find how many cases I need to create to write robust code ?

You have the three cases described in the instructions:

  • If the year is divisible by 4, then it is a leap year.
  • Unless the year is also divisible by 100, then it is not a leap year.
  • Unless the year is also divisible by 400, then it is a leap year.

To which you would need to add the one that is implicit: when the year is not divisible by 4

done, test passed,

solution removed by moderator

thank you @igorgetmeabrain and @ILM

1 Like