Skip to content

Commit 80e6ba5

Browse files
committed
🐛 fix: removing duplicated code
1 parent feae726 commit 80e6ba5

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

Dynamic-Programming/FindMonthCalendar.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* And prints out the month's calendar.
44
* It uses an epoch of 1/1/1900, Monday.
55
*/
6+
import { isLeapYear } from '../Maths/LeapYear.js'
67

78
class Month {
89
constructor () {
@@ -51,10 +52,6 @@ class Month {
5152
return dateOb
5253
}
5354

54-
isLeapYear (year) {
55-
return ((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))
56-
}
57-
5855
isGreater (startDate, endDate) {
5956
if (startDate.year > endDate.year) {
6057
return true
@@ -78,16 +75,16 @@ class Month {
7875
}
7976
let diff = 0
8077
while (startDate.year !== endDate.year) {
81-
diff += (this.isLeapYear(startDate.year)) ? 366 : 365
78+
diff += (isLeapYear(startDate.year)) ? 366 : 365
8279
startDate.year = startDate.year + 1
8380
}
8481
while (startDate.month !== endDate.month) {
8582
if (startDate.month < endDate.month) {
86-
if (this.isLeapYear(startDate.year)) diff += this.monthDaysLeap[startDate.month]
83+
if (isLeapYear(startDate.year)) diff += this.monthDaysLeap[startDate.month]
8784
else diff += this.monthDays[startDate.month]
8885
startDate.month = startDate.month + 1
8986
} else {
90-
if (this.isLeapYear(startDate.year)) diff -= this.monthDaysLeap[startDate.month - 1]
87+
if (isLeapYear(startDate.year)) diff -= this.monthDaysLeap[startDate.month - 1]
9188
else diff -= this.monthDays[startDate.month - 1]
9289
startDate.month = startDate.month - 1
9390
}
@@ -102,7 +99,7 @@ class Month {
10299
let Month2 = this.parseDate(date)
103100
day = (this.isGreater(Month2, this.epoch)) ? this.Days[difference] : this.BDays[difference]
104101
Month2 = this.parseDate(date)
105-
if (this.isLeapYear(Month2.year)) this.printCal(this.monthDaysLeap[Month2.month], day)
102+
if (isLeapYear(Month2.year)) this.printCal(this.monthDaysLeap[Month2.month], day)
106103
else this.printCal(this.monthDays[Month2.month], day)
107104
}
108105
}

0 commit comments

Comments
 (0)