X Tutup
Skip to content

Commit 7e419f0

Browse files
authored
chore: Merge pull request TheAlgorithms#726 from ahamed/master
✨ add an algorithm for checking a year is a leap year or not.
2 parents 2534858 + b23f9af commit 7e419f0

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
* [IsDivisible](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsDivisible.js)
157157
* [IsEven](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsEven.js)
158158
* [isOdd](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isOdd.js)
159+
* [LeapYear](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/LeapYear.js)
159160
* [Mandelbrot](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Mandelbrot.js)
160161
* [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js)
161162
* [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js)

Maths/LeapYear.js

Lines changed: 22 additions & 0 deletions
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

Lines changed: 22 additions & 0 deletions
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+
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"jest": "^26.4.2",
3232
"standard": "^14.3.4"
3333
}
34-
}
34+
}

0 commit comments

Comments
 (0)
X Tutup