mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
fix: properly floor the partial results (#1629)
This commit is contained in:
@ -14,21 +14,27 @@ const isLeap = (year) => {
|
||||
else return false
|
||||
}
|
||||
const DateToDay = (dd, mm, yyyy) => {
|
||||
return Math.floor(
|
||||
return (
|
||||
365 * (yyyy - 1) +
|
||||
(yyyy - 1) / 4 -
|
||||
(yyyy - 1) / 100 +
|
||||
(yyyy - 1) / 400 +
|
||||
dd +
|
||||
(367 * mm - 362) / 12 +
|
||||
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
|
||||
Math.floor((yyyy - 1) / 4) -
|
||||
Math.floor((yyyy - 1) / 100) +
|
||||
Math.floor((yyyy - 1) / 400) +
|
||||
dd +
|
||||
Math.floor((367 * mm - 362) / 12) +
|
||||
(mm <= 2 ? 0 : isLeap(yyyy) ? -1 : -2)
|
||||
)
|
||||
}
|
||||
|
||||
const CheckDayAndMonth = (inDay, inMonth) => {
|
||||
if (inDay <= 0 || inDay > 31 || inMonth <= 0 || inMonth > 12) {
|
||||
throw new TypeError('Date is not valid.')
|
||||
}
|
||||
}
|
||||
|
||||
const DateDayDifference = (date1, date2) => {
|
||||
// firstly, check that both input are string or not.
|
||||
if (typeof date1 !== 'string' || typeof date2 !== 'string') {
|
||||
return new TypeError('Argument is not a string.')
|
||||
throw new TypeError('Argument is not a string.')
|
||||
}
|
||||
// extract the first date
|
||||
const [firstDateDay, firstDateMonth, firstDateYear] = date1
|
||||
@ -39,18 +45,9 @@ const DateDayDifference = (date1, date2) => {
|
||||
.split('/')
|
||||
.map((ele) => Number(ele))
|
||||
// check the both data are valid or not.
|
||||
if (
|
||||
firstDateDay < 0 ||
|
||||
firstDateDay > 31 ||
|
||||
firstDateMonth > 12 ||
|
||||
firstDateMonth < 0 ||
|
||||
secondDateDay < 0 ||
|
||||
secondDateDay > 31 ||
|
||||
secondDateMonth > 12 ||
|
||||
secondDateMonth < 0
|
||||
) {
|
||||
return new TypeError('Date is not valid.')
|
||||
}
|
||||
CheckDayAndMonth(firstDateDay, firstDateMonth)
|
||||
CheckDayAndMonth(secondDateDay, secondDateMonth)
|
||||
|
||||
return Math.abs(
|
||||
DateToDay(secondDateDay, secondDateMonth, secondDateYear) -
|
||||
DateToDay(firstDateDay, firstDateMonth, firstDateYear)
|
||||
|
@ -1,21 +1,36 @@
|
||||
import { DateDayDifference } from '../DateDayDifference'
|
||||
|
||||
test('The difference between 17/08/2002 & 10/10/2020 is 6630', () => {
|
||||
const res = DateDayDifference('17/08/2002', '10/10/2020')
|
||||
expect(res).toBe(6630)
|
||||
})
|
||||
describe('DateDayDifference', () => {
|
||||
it.each([
|
||||
['17/08/2002', '10/10/2020', 6629],
|
||||
['18/02/2001', '16/03/2022', 7696],
|
||||
['11/11/2011', '12/12/2012', 397],
|
||||
['01/01/2001', '16/03/2011', 3726],
|
||||
['04/03/2024', '04/03/2024', 0],
|
||||
['03/03/2024', '04/03/2024', 1],
|
||||
['02/03/2024', '04/03/2024', 2],
|
||||
['01/03/2024', '04/03/2024', 3],
|
||||
['29/02/2024', '04/03/2024', 4],
|
||||
['04/03/2024', '04/03/2025', 365],
|
||||
['04/03/2023', '04/03/2024', 366]
|
||||
])(
|
||||
'The difference between %s and %s is %i',
|
||||
(firstDate, secondDate, expected) => {
|
||||
expect(DateDayDifference(firstDate, secondDate)).toBe(expected)
|
||||
expect(DateDayDifference(secondDate, firstDate)).toBe(expected)
|
||||
}
|
||||
)
|
||||
|
||||
test('The difference between 18/02/2001 & 16/03/2022 is 7696', () => {
|
||||
const res = DateDayDifference('18/02/2001', '16/03/2022')
|
||||
expect(res).toBe(7696)
|
||||
})
|
||||
it('should throw when any input is not a string', () => {
|
||||
expect(() => DateDayDifference(10102024, '11/10/2024')).toThrowError()
|
||||
expect(() => DateDayDifference('11/10/2024', 10102024)).toThrowError()
|
||||
})
|
||||
|
||||
test('The difference between 11/11/2011 & 12/12/2012 is 398', () => {
|
||||
const res = DateDayDifference('11/11/2011', '12/12/2012')
|
||||
expect(res).toBe(398)
|
||||
})
|
||||
|
||||
test('The difference between 01/01/2001 & 16/03/2011 is 3727', () => {
|
||||
const res = DateDayDifference('01/01/2001', '16/03/2011')
|
||||
expect(res).toBe(3727)
|
||||
it.each(['32/01/2000', '00/01/2000', '15/00/2000', '15/13/2000'])(
|
||||
'should throw when input is not a correct date %s',
|
||||
(wrongDate) => {
|
||||
expect(() => DateDayDifference(wrongDate, '04/03/2024')).toThrowError()
|
||||
expect(() => DateDayDifference('04/03/2024', wrongDate)).toThrowError()
|
||||
}
|
||||
)
|
||||
})
|
||||
|
Reference in New Issue
Block a user