Skip to content

Commit 0f703a5

Browse files
committed
Added Month calendar finder
Added a dynamic programming algorithm to print out a Month's calendar.
1 parent e0d2e5a commit 0f703a5

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* This algorithm accepts a month in the format mm/yyyy.
3+
* And prints out the month's calendar.
4+
* It uses an epoch of 1/1/1900, Monday.
5+
*/
6+
7+
class Month {
8+
constructor () {
9+
this.Days = ['M', 'T', 'W', 'Th', 'F', 'S', 'Su']
10+
this.BDays = ['M', 'Su', 'S', 'F', 'Th', 'W', 'T']
11+
this.epoch = { month: 1, year: 1900 }
12+
this.monthDays = [31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
13+
this.monthDaysLeap = [31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
14+
}
15+
16+
printCal (days, startDay) {
17+
console.log('M T W Th F S Su')
18+
const dates = []; let i
19+
for (i = 1; i <= days; i++) {
20+
dates.push(i)
21+
}
22+
for (i = 0; i < this.Days.indexOf(startDay); i++) {
23+
dates.unshift(' ')
24+
}
25+
while (true) {
26+
let row = ''
27+
for (i = 0; (i < 7) && (dates.length !== 0); i++) {
28+
row += dates.shift()
29+
while ((row.length % 4) !== 0) {
30+
row += ' '
31+
}
32+
}
33+
console.log(row)
34+
if (dates.length === 0) break
35+
}
36+
}
37+
38+
parseDate (date) {
39+
const dateAr = []; let block = ''; let i
40+
for (i = 0; i < date.length; i++) {
41+
if (date[i] === '/') {
42+
dateAr.push(parseInt(block))
43+
block = ''
44+
continue
45+
}
46+
block += date[i]
47+
}
48+
dateAr.push(parseInt(block))
49+
if (dateAr.length !== 2) throw new Error('Improper string encoding')
50+
const dateOb = { month: dateAr[0], year: dateAr[1] }
51+
return dateOb
52+
}
53+
54+
isLeapYear (year) {
55+
if (((year % 400) === 0) || (((year % 100) !== 0) && ((year % 4) === 0))) return true
56+
else return false
57+
}
58+
59+
isGreater (startDate, endDate) {
60+
if (startDate.year > endDate.year) { return true } else if (startDate.year < endDate.year) { return false } else if (startDate.month > endDate.month) { return true } else if (startDate.month < endDate.month) { return false } else return true
61+
}
62+
63+
getDayDiff (startDate, endDate) {
64+
if (this.isGreater(startDate, endDate) === null) {
65+
return 0
66+
} else if ((this.isGreater(startDate, endDate) === true)) {
67+
const midDate = startDate
68+
startDate = endDate
69+
endDate = midDate
70+
}
71+
let diff = 0
72+
while (startDate.year !== endDate.year) {
73+
diff += (this.isLeapYear(startDate.year)) ? 366 : 365
74+
startDate.year = startDate.year + 1
75+
}
76+
while (startDate.month !== endDate.month) {
77+
if (startDate.month < endDate.month) {
78+
if (this.isLeapYear(startDate.year)) diff += this.monthDaysLeap[startDate.month]
79+
else diff += this.monthDays[startDate.month]
80+
startDate.month = startDate.month + 1
81+
} else {
82+
if (this.isLeapYear(startDate.year)) diff -= this.monthDaysLeap[startDate.month - 1]
83+
else diff -= this.monthDays[startDate.month - 1]
84+
startDate.month = startDate.month - 1
85+
}
86+
}
87+
return diff
88+
}
89+
90+
generateMonthCal (date) {
91+
const Month = this.parseDate(date); let day = ''
92+
let difference = this.getDayDiff(this.epoch, Month)
93+
difference = difference % 7
94+
let Month2 = this.parseDate(date)
95+
day = (this.isGreater(Month2, this.epoch)) ? this.Days[difference] : this.BDays[difference]
96+
Month2 = this.parseDate(date)
97+
if (this.isLeapYear(Month2.year)) this.printCal(this.monthDaysLeap[Month2.month], day)
98+
else this.printCal(this.monthDays[Month2.month], day)
99+
}
100+
}
101+
102+
103+
//testing
104+
const x = new Month()
105+
x.generateMonthCal('1/2021')

0 commit comments

Comments
 (0)