3

I'm trying to determine whether a year is a leap year or not. I'm not sure where i'm missing something because this code is meant to determine that.

Thanks for your help.

let Year = (year) => {
    this.year = year;
};

Year.prototype.isLeap = () => {
    return (
        this.year % 400 === 0 ||
        (this.year % 4 === 0 && (this.year % 100 === 0))
    );
};

let year = new Year(2014);

year.isLeap();

Thanks I've figured it out.

Initially i did it will the kind of If statement you guys are pointing to here!, so I'm now refactoring to av a cleaner code.

My code was having issue on this line

(this.year % 4 === 0 && (this.year % 100 === 0))

the right syntax is

(this.year % 4 === 0 && !(this.year % 100 === 0))
Mindsworth
  • 37
  • 2
  • 10
  • 1
    Use a standard function instead of an arrow function in order to capture the calling context (both in `Year` and `isLeap`) – CertainPerformance May 10 '18 at 03:28
  • 1
    Possible duplicate of [Writing a JavaScript program to calculate a leap year](https://stackoverflow.com/questions/36073272/writing-a-javascript-program-to-calculate-a-leap-year) – Mark May 10 '18 at 03:29
  • 1
    Your logic is wrong anyway ... it thinks that leap years are only 1900,2000,2100, etc – Jaromanda X May 10 '18 at 03:32
  • `this.year % 4 === 0 && (this.year % 100 !==0 || this.year %400 == 0)` – Jaromanda X May 10 '18 at 03:35
  • This question already has an answer here: https://stackoverflow.com/questions/16353211/check-if-year-is-leap-year-in-javascript – hoangkianh May 10 '18 at 03:37
  • which is a duplicate of https://stackoverflow.com/questions/8175521/javascript-to-find-leap-year :p – Jaromanda X May 10 '18 at 03:38
  • Possible duplicate of [javascript to find leap year](https://stackoverflow.com/questions/8175521/javascript-to-find-leap-year) – deblocker May 10 '18 at 04:35
  • One line: `return ((this.year & 3) == 0 && ((this.year % 25) != 0 || (this.year & 15) == 0));` – Kevin P. Rice Dec 10 '18 at 11:29

4 Answers4

8

You could just check the feburary 29th of the given year and see if its changes to march 1st.

const date = new Date(this.year, 1, 29);
return date.getMonth() === 1;

If getMonth() returns 1, then its still feburary which means its leap year.

brenjt
  • 15,997
  • 13
  • 77
  • 118
0
Number.prototype.isLeap = function() {
  return !(this % 4 || !(this % 100) && this % 400);
}

let year = 2000;
console.log(year.isLeap()); // prints true

year = 1900;
console.log(year.isLeap()); // prints false

year = 1904;
console.log(year.isLeap()); // prints true

year = 2003;
console.log(year.isLeap()); // prints false
0

The following code block will work well on Javascript and also on Typescript if you remove the function keyword. To understand the logic behind this implementation have a look at this link How to determine whether a year is a leap year.

 function isLeapYear(year) {
    let isLeapObj = {};
    if ((year % 4 === 0 && year % 100 != 0) || year % 400 === 0) {
      isLeapObj['isLeap'] = true;
      isLeapObj['days'] = 366;
    } else {
      isLeapObj['isLeap'] = false;
      isLeapObj['days'] = 365;
    }
    return isLeapObj;
  }

x = isLeapYear(2020);
console.log(x);

For Javscript use the following code

Hamfri
  • 1,979
  • 24
  • 28
0

In regards to @brenjt's answer above you might want to change the value 29 to 30

const date = new Date(this.year, 1, 30);
if (date.getMonth() === 1) {
  console.log("it's not a leap year");
} else {
  console.log("it's a leap year");
}
Samuelson Okoi
  • 111
  • 1
  • 5