mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
✨ add an algorithm for checking a year is a leap year or not
This commit is contained in:
22
Maths/LeapYear.js
Normal file
22
Maths/LeapYear.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* isLeapYear :: Number -> Boolean
|
||||||
|
*
|
||||||
|
* Check if a year is a leap year or not. A leap year is a year which has 366 days.
|
||||||
|
* For the extra +1 day the February month contains 29 days instead of 28 days.
|
||||||
|
*
|
||||||
|
* The logic behind the leap year is-
|
||||||
|
* 1. If the year is divisible by 400 then it is a leap year.
|
||||||
|
* 2. If it is not divisible by 400 but divisible by 100 then it is not a leap year.
|
||||||
|
* 3. If the year is not divisible by 400 but not divisible by 100 and divisible by 4 then a leap year.
|
||||||
|
* 4. Other cases except the describing ones are not a leap year.
|
||||||
|
*
|
||||||
|
* @param {number} year
|
||||||
|
* @returns {boolean} true if this is a leap year, false otherwise.
|
||||||
|
*/
|
||||||
|
export const isLeapYear = (year) => {
|
||||||
|
if (year % 400 === 0) return true
|
||||||
|
if (year % 100 === 0) return false
|
||||||
|
if (year % 4 === 0) return true
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
22
Maths/test/LeapYear.test.js
Normal file
22
Maths/test/LeapYear.test.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { isLeapYear } from '../LeapYear'
|
||||||
|
|
||||||
|
describe('Leap Year', () => {
|
||||||
|
it('Should return true on the year 2000', () => {
|
||||||
|
expect(isLeapYear(2000)).toBe(true)
|
||||||
|
})
|
||||||
|
it('Should return false on the year 2001', () => {
|
||||||
|
expect(isLeapYear(2001)).toBe(false)
|
||||||
|
})
|
||||||
|
it('Should return false on the year 2002', () => {
|
||||||
|
expect(isLeapYear(2002)).toBe(false)
|
||||||
|
})
|
||||||
|
it('Should return false on the year 2003', () => {
|
||||||
|
expect(isLeapYear(2003)).toBe(false)
|
||||||
|
})
|
||||||
|
it('Should return false on the year 2004', () => {
|
||||||
|
expect(isLeapYear(2004)).toBe(true)
|
||||||
|
})
|
||||||
|
it('Should return false on the year 1900', () => {
|
||||||
|
expect(isLeapYear(1900)).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
12437
package-lock.json
generated
12437
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -12,10 +12,10 @@
|
|||||||
"@babel/core": "^7.11.6",
|
"@babel/core": "^7.11.6",
|
||||||
"@babel/plugin-transform-runtime": "^7.11.5",
|
"@babel/plugin-transform-runtime": "^7.11.5",
|
||||||
"@babel/preset-env": "^7.11.5",
|
"@babel/preset-env": "^7.11.5",
|
||||||
|
"atob": "2.1.2",
|
||||||
"jsdom": "^16.3.0",
|
"jsdom": "^16.3.0",
|
||||||
"node": "^14.13.1",
|
"node": "^14.13.1",
|
||||||
"node-fetch": "2.6.1",
|
"node-fetch": "2.6.1"
|
||||||
"atob": "2.1.2"
|
|
||||||
},
|
},
|
||||||
"standard": {
|
"standard": {
|
||||||
"env": [
|
"env": [
|
||||||
|
Reference in New Issue
Block a user