← Back to challenges

Leap Years

JavaScriptHardmathvalidationlogicdates

Instructions

A leap year has one day added to February for being synchronized with the seasonal year. A leap year appears with a regular frequency, which is determined by the rule below:

  • A year must either be divisible by 400 or divisible by 4 and not 100.

Given a year you must implement a function that returns true if it's a leap year, or false if it's not.

Examples

isLeap(2020) ➞ true
// Exactly divided by 4 and not by 100.

isLeap(1800) ➞ false
// Exactly divided by 4, but is also exactly divided by 100.

isLeap(2000) ➞ true
// Exactly divided by 400.

isLeap(2019) ➞ false
// It can't be exactly divided by 400 or by 4.

Notes

  • Exactly divided can be interpreted as the remainder of the division is equal to 0.

  • Bonus: Try to implement this function writing a single line of code.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Alphabet Soup