-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (108 loc) · 3.96 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
let inputDate = document.querySelector('#inputDate');
let form = document.querySelector('#form');
let resultDiv = document.querySelector('#result');
let footerDiv = document.querySelector('footer');
let splitDate, dateArray, firstFormat, secondFormat, thirdFormat, showOutput;
const daysOfMonth = [31, Number(`${new Date().getFullYear() % 4 ? 29 : 28}`), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
form.addEventListener('submit', (e) => {
e.preventDefault();
resultDiv.innerText = '';
result.style.margin = 'auto';
dateArray = inputDate.value.split('-');
year = dateArray[0].toString();
month = dateArray[1].toString();
date = dateArray[2].toString();
showOutput = formats(date, month, year);
resultDiv.style.display = 'none';
footerDiv.scrollIntoView({behavior: "smooth", bottom: 0});
setTimeout(() => {
if(showOutput) {
resultDiv.style.display = 'block';
resultDiv.innerHTML = `Yayy! Your birthday is palindrome in the format <b>${showOutput}</b>.`;
resultDiv.scrollIntoView({behavior: "smooth", bottom: 0});
} else {
paliArray = findNextPalindrome(inputDate.value);
resultDiv.style.display = 'block';
resultDiv.innerHTML = `OOPS! Your birthday is not a palindrome. The nearest palindrome is <b>${paliArray[0]}</b> and you missed it by <b>${paliArray[1]}</b> days.`;
resultDiv.scrollIntoView({behavior: "smooth", bottom: 0});
}
}, 350);
});
function formats(date, month, year) {
if(date.length === 1) {
date = '0' + date;
}
if(month.length === 1) {
month = '0' + month;
}
firstFormat = year + month + date;
secondFormat = month + date + year;
thirdFormat = date + month + year;
if(checkIfPalindrome(firstFormat)) {
return (`${year}-${month}-${date}`);
}
else if(checkIfPalindrome(secondFormat)) {
return (`${month}-${date}-${year}`);
}
else if(checkIfPalindrome(thirdFormat)) {
return (`${date}-${month}-${year}`);
}
else {
return null;
}
}
function checkIfPalindrome(format) {
if(format.toString().split('').reverse().join('') === format) {
return true;
}
}
function findNextPalindrome(inputDate) {
split = inputDate.split('-');
nextDate = Number(split[2]);
nextMonth = Number(split[1]);
nextYear = Number(split[0]);
prevDate = Number(split[2]);
prevMonth = Number(split[1]);
prevYear = Number(split[0]);
console.log(prevDate, prevMonth, prevYear, '==', nextDate, nextMonth, nextYear);
daysElapsed = 0;
while(true) {
daysElapsed += 1;
nextDate += 1;
prevDate -= 1;
if(nextDate > daysOfMonth[nextMonth - 1]) {
nextDate = 1;
nextMonth += 1;
if(nextMonth > 12) {
nextMonth = 1;
nextYear += 1;
if(nextYear > 9999) {
break;
}
}
}
if(prevDate < 1) {
prevMonth -= 1;
if(prevMonth < 1) {
prevYear -= 1;
if(prevYear < 1) {
return ['', ''];
} else {
prevMonth = 12;
prevDate = daysOfMonth[prevMonth - 1];
}
} else {
prevDate = daysOfMonth[prevMonth - 1];
}
}
console.log(prevDate, prevMonth, prevYear, '==', nextDate, nextMonth, nextYear);
prevFormat = formats(prevDate.toString(), prevMonth.toString(), prevYear.toString());
if(prevFormat) {
return [prevFormat, daysElapsed];
}
nextFormat = formats(nextDate.toString(), nextMonth.toString(), nextYear.toString());
if(nextFormat) {
return [nextFormat, daysElapsed];
}
}
}