Build a leap year calculator test

Does anyone know how to get these two failing tests on the ‘build a leap year’ project to pass (see screenshot)? I’m not sure how to assign $year to the three years at the same time. Any help would be great. Thanks!

Andrew.

Hi.

It’s hard for us to work with screenshots. We will need to see your code.

Welcome to the forum @admsmc

What happens when you change the year variable to 2000?

Happy coding

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

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

Thank you @Teller - it’s my first ever coding forum :slight_smile:

When I change the $year to 2000. The 2000 test passes but the 2024 and 1900 tests fail and I can not “complete” the lab. Same when I change the $year to 1900, the 2024 and 2000 tests fail.

Hi @stephenmutheu - apologies, I didn’t realize that. Have posted in the code in a reply above. Thanks!

does that tell you something is going wrong with your function?

Hi @ILM - I figure something is wrong with my function as the tests are not passing - two of the three always fail depending on if I set $year to 2024, 2000, or 1900. Probably I’m missing something really dumb (new to coding), and help would be great!

Changing a global variable should not change how this function runs. So what might be preventing the function from returning correct values based on the function argument?

Hi @JeremyLT -

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

const year = 2024;

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

fixed my silly mistake - thank you for the help!!

Yes I was missing something really dumb but got these in the end!