Skip to content

Commit bf088c0

Browse files
committed
✨ add an algorithm for checking a year is a leap year or not
1 parent c1b6fca commit bf088c0

File tree

4 files changed

+12475
-10
lines changed

4 files changed

+12475
-10
lines changed

Maths/LeapYear.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* isLeapYear :: Number -> Boolean
3+
*
4+
* Check if a year is a leap year or not. A leap year is a year which has 366 days.
5+
* For the extra +1 day the February month contains 29 days instead of 28 days.
6+
*
7+
* The logic behind the leap year is-
8+
* 1. If the year is divisible by 400 then it is a leap year.
9+
* 2. If it is not divisible by 400 but divisible by 100 then it is not a leap year.
10+
* 3. If the year is not divisible by 400 but not divisible by 100 and divisible by 4 then a leap year.
11+
* 4. Other cases except the describing ones are not a leap year.
12+
*
13+
* @param {number} year
14+
* @returns {boolean} true if this is a leap year, false otherwise.
15+
*/
16+
export const isLeapYear = (year) => {
17+
if (year % 400 === 0) return true
18+
if (year % 100 === 0) return false
19+
if (year % 4 === 0) return true
20+
21+
return false
22+
}

Maths/test/LeapYear.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { isLeapYear } from '../LeapYear'
2+
3+
describe('Leap Year', () => {
4+
it('Should return true on the year 2000', () => {
5+
expect(isLeapYear(2000)).toBe(true)
6+
})
7+
it('Should return false on the year 2001', () => {
8+
expect(isLeapYear(2001)).toBe(false)
9+
})
10+
it('Should return false on the year 2002', () => {
11+
expect(isLeapYear(2002)).toBe(false)
12+
})
13+
it('Should return false on the year 2003', () => {
14+
expect(isLeapYear(2003)).toBe(false)
15+
})
16+
it('Should return false on the year 2004', () => {
17+
expect(isLeapYear(2004)).toBe(true)
18+
})
19+
it('Should return false on the year 1900', () => {
20+
expect(isLeapYear(1900)).toBe(false)
21+
})
22+
})

0 commit comments

Comments
 (0)