Build a Leap Year Calculator - Build a Leap Year Calculator

Tell us what’s happening:

My Leap Year Calculator has a broken if/else loop. For some reason, everything is a leap year, even odd number years. I’ve checked the formatting of my if/else conditional and can’t find anything wrong with it. The only thing I can find that has a problem is my year variable. Obviously, it doesn’t update for some reason. The first year stays the same even if I update the code later on. However, if I update the first year to an odd year, I still am told that it is a leap year.

Your code so far

function isLeapYear(year) {
  
  if(div4 && div400) {
    return year + " is a leap year.";
  }
  else if(div4 && div100 && (!div400)) {
    return year + " is not a leap year.";
  }
  else if(div4 && (!div100)) {
    return year + " is a leap year.";
  }
  else if(!div4) {
    return year + " is not a leap year.";
  }
  else {
    "Enter a year."
  }
}

function div4(year) {
  if((year % 4) === 0) {
    return true;
  }
  else {
    return false;
  }
}

function div400(year) {
  if((year % 400) === 0) {
    return true;
  }
  else {
    return false;
  }
}

function div100(year) {
  if((year % 100) === 0) {
    return true;
  }
  else {
    return false;
  }
}

let year;

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

year = 2000;
console.log(result);

year = 1900;
console.log(result);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0

Challenge Information:

Build a Leap Year Calculator - Build a Leap Year Calculator

Hi,

You have two issues. Notice in your if statements, you’re using div4 ,div100 and div400 as if they are boolens. But they are not, they are functions, so you need to call the functions inside each of your if else statements. Don’t forget to pass year as a perimeter when calling the functions.

And second, you have the variable result that calls the function, but you’re using it after year=2024 , and that causes the result to never change, even if you change the year. Try setting result everytime you change the year variable.

1 Like