Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Maths/CheckLeapYear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @function checkLeapYear
* @description A leap year is a year that is divisible by 4, except for
* century years which must be divisible by 400. This function checks
* whether a given year is a leap year.
* @param {number} year - A positive integer representing a year
* @returns {boolean} - True if the year is a leap year, false otherwise
* @see https://en.wikipedia.org/wiki/Leap_year
* @example checkLeapYear(2000) => true
* @example checkLeapYear(1900) => false
* @example checkLeapYear(2024) => true
*/
const checkLeapYear = (year) => {
if (!Number.isInteger(year) || year < 1) {
throw new TypeError('Input must be a positive integer')
}
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0
}

export { checkLeapYear }
47 changes: 47 additions & 0 deletions Maths/test/CheckLeapYear.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { checkLeapYear } from '../CheckLeapYear.js'

describe('checkLeapYear', () => {
test('2000 is a leap year', () => {
expect(checkLeapYear(2000)).toBe(true)
})

test('1900 is NOT a leap year', () => {
expect(checkLeapYear(1900)).toBe(false)
})

test('2024 is a leap year', () => {
expect(checkLeapYear(2024)).toBe(true)
})

test('2023 is NOT a leap year', () => {
expect(checkLeapYear(2023)).toBe(false)
})

test('1600 is a leap year', () => {
expect(checkLeapYear(1600)).toBe(true)
})

test('1700 is NOT a leap year', () => {
expect(checkLeapYear(1700)).toBe(false)
})

test('2100 is NOT a leap year', () => {
expect(checkLeapYear(2100)).toBe(false)
})

test('400 is a leap year', () => {
expect(checkLeapYear(400)).toBe(true)
})

test('throws TypeError for zero', () => {
expect(() => checkLeapYear(0)).toThrow(TypeError)
})

test('throws TypeError for negative input', () => {
expect(() => checkLeapYear(-4)).toThrow(TypeError)
})

test('throws TypeError for float input', () => {
expect(() => checkLeapYear(2024.5)).toThrow(TypeError)
})
})
Loading