-
Notifications
You must be signed in to change notification settings - Fork 36
/
app.js
218 lines (185 loc) · 4.79 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
function reverseStr(str) {
return str.split('').reverse().join('');
}
function isPalindrome(str) {
if(str === reverseStr(str)) return true;
else return false;
}
function convertDateToStr(date) {
var dateStr = {day: '', month: '', year: ''};
if(date.day<10) {
dateStr.day = '0' + date.day;
}
else {
dateStr.day = date.day.toString();
}
if(date.month <10) {
dateStr.month = '0' + date.month;
}
else {
dateStr.month = date.month.toString();
}
dateStr.year = date.year.toString();
return dateStr;
}
function getAllDateFormats(date) {
var dateStr = convertDateToStr(date);
var ddmmyyyy = dateStr.day + dateStr.month + dateStr.year;
var mmddyyyy = dateStr.month + dateStr.day + dateStr.year;
var yyyymmdd = dateStr.year + dateStr.month + dateStr.day;
var ddmmyy = dateStr.day + dateStr.month + dateStr.year.slice(-2);
var mmddyy = dateStr.month + dateStr.day + dateStr.year.slice(-2);
var yymmdd = dateStr.year.slice(-2) + dateStr.month + dateStr.day;
return [ddmmyyyy, mmddyyyy, yyyymmdd, ddmmyy, mmddyy, yymmdd];
}
function checkPalindromeForAllFormats(date) {
var allFormats = getAllDateFormats(date);
var palindrome = false;
for(var i=0;i<allFormats.length;i++){
if(isPalindrome(allFormats[i])){
palindrome = true;
}
}
return palindrome;
}
function isLeapYear(year) {
if(year % 4 === 0 ) {
return true;
}
if(year % 100 === 0 ) {
return false;
}
if(year % 4 === 0) {
return true;
}
return false;
}
var daysInMonth = [31,28,31,30,31,30,31,31,30,31,30,31];
function getNextDate(date) {
var day = date.day + 1;
var month = date.month;
var year = date.year;
if(month === 2){
if(isLeapYear(year)){
if(day > 29 ) {
day = 1;
month++;
}
} else {
if(day > 28 ){
day = 1;
month++;
}
}
} else {
if(day > daysInMonth[month - 1]){
day = 1;
month++;
}
}
if(month > 12 ) {
month = 1;
year++;
}
return {
day: day,
month: month,
year: year
}
}
function getPrevDate(date) {
var day = date.day - 1;
var month = date.month;
var year = date.year;
if(month === 3) {
if(isLeapYear(year)) {
if(day < 1){
day = 29;
month--;
}
} else {
if(day < 1) {
day = 28;
month--;
}
}
} else {
if(month === 1 && day<1){
day = 31;
month = 12;
year--;
}
if(month > 1 && day < 1){
day = daysInMonth[month - 2];
month--;
}
}
return {
day: day,
month: month,
year: year
}
}
function getNextPalindromeDate(date) {
var count = 0;
var nextDate = getNextDate(date);
while(1) {
count++;
var isPalindrome = checkPalindromeForAllFormats(nextDate);
if(isPalindrome){
break;
}
nextDate = getNextDate(nextDate);
}
return [count, nextDate];
}
function getPreviousPalindromeDate(date) {
var previousDate = getPrevDate(date);
let counter = 0;
while(1) {
counter++;
var isPalindrome = checkPalindromeForAllFormats(previousDate);
if(isPalindrome) {
break;
}
previousDate = getPrevDate(previousDate);
}
return [counter, previousDate];
}
var date = {
day: 11,
month: 09,
year: 2022
}
console.log(getNextPalindromeDate(date));
var dateInput = document.querySelector('#bday-input');
var showBtn = document.querySelector('.show-btn');
var result = document.querySelector('.result')
showBtn.addEventListener('click',(e) => {
console.log(dateInput.value);
})
function clickHandler(e) {
var bdayStr = dateInput.value;
if(bdayStr !== '') {
var strDate = bdayStr.split('-');
var date = {
day: Number(strDate[2]),
month: Number(strDate[1]),
year: Number(strDate[0])
}
var isPalindrome = checkPalindromeForAllFormats(date);
// console.log(isPalindrome)
if(isPalindrome) {
result.innerText = 'Yay! your birthday is a palindrome! ✨🥳'
}
else {
var [countNext, nextDate] = getNextPalindromeDate(date);
var [countPrev, prevDate] = getPreviousPalindromeDate(date);
result.innerHTML = `<li>No, your birthday is <span>Not</span> a Palindrome.🥲☹️</li><br>
<li>You just missed by <span>${countNext}</span> days, the next palindrome date is <span>${nextDate.day}-${nextDate.month}-${nextDate.year}</span>
</li><br>
<li>Also, The Previous palindrome date is <span>${prevDate.day}-${prevDate.month}-${prevDate.year}</span>, you just missed by <span>${countPrev}</span> days.</li>`
}
}
}
showBtn.addEventListener('click',clickHandler);