-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
212 lines (173 loc) · 7.76 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
// getting all varibles needed inside the document
const daysInput = document.getElementById('days')
const monthInput = document.getElementById('months')
const yearsInput = document.getElementById('years')
const datesSubmitBtn = document.getElementById('myage')
//getting where the dates will displayed
const yearAge = document.getElementById('years-age')
const monthAge = document.getElementById('months-age')
const dayAge = document.getElementById('days-age')
// error text from the document
const onErrorDayDisplay = document.getElementById('wrong-day-input')
const onErrorMonthDisplay = document.getElementById('wrong-month-input')
const onErrorYearDisplay = document.getElementById('wrong-year-input')
//my current dates
const currentDate = new Date()
//user display style
const onErrorClrDisplayDay = document.getElementById('onError-day')
const onErrorClrDisplayMonth = document.getElementById('onError-month')
const onErrorClrDisplayYear = document.getElementById('onError-year')
//months with days
const monthsWithDays = {
0: 31,
1: 29,
2: 31,
3: 30,
4: 31,
5: 30,
6: 31,
7: 31,
8: 30,
9: 31,
10: 30,
11: 31
};
let isError = false; // Variable to track error state
//function to calculate the age
async function calculateAge() {
let userYearInput = yearsInput.value
let userMonthInput = monthInput.value
let userDayInput = daysInput.value
return new Promise((resolve) => {
//setting all boxes to check errors before any calulations begins
if (isNaN(userYearInput) ) {
isError = true;
console.log('not a number');
onErrorYearDisplay.classList.add('transition-all')
onErrorYearDisplay.style.display = 'block'
onErrorClrDisplayYear.classList.add('error-clr-display');
yearsInput.style.borderColor = '#FF5959';
}
else if (userYearInput.trim() === '' || userYearInput > currentDate.getFullYear() || userYearInput < 0) {
isError = true;
console.log('input box is empty');
onErrorYearDisplay.classList.add('transition-all')
onErrorYearDisplay.style.display = 'block'
onErrorClrDisplayYear.classList.add('error-clr-display');
yearsInput.style.borderColor = '#FF5959';
}
else if (isNaN(userMonthInput)) {
isError = true;
console.log('not a number');
onErrorMonthDisplay.classList.add('transition-all')
onErrorMonthDisplay.style.display = 'block'
onErrorClrDisplayMonth.classList.add('error-clr-display');
monthInput.style.borderColor = '#FF5959';
}
else if (userMonthInput.trim() === '' || userMonthInput > 12 || userMonthInput < 0) {
isError = true;
console.log('not a number');
onErrorMonthDisplay.classList.add('transition-all')
onErrorMonthDisplay.style.display = 'block'
onErrorClrDisplayMonth.classList.add('error-clr-display');
monthInput.style.borderColor = '#FF5959';
}
else if (isNaN(userDayInput)) {
isError = true;
console.log('not a number');
onErrorDayDisplay.classList.add('transition-all')
onErrorDayDisplay.style.display = 'block'
onErrorClrDisplayDay.classList.add('error-clr-display');
daysInput.style.borderColor = '#FF5959';
}
else if (userDayInput.trim() === '' || userDayInput > 31 || userDayInput < 0 ) {
isError = true;
console.log('not a number');
onErrorDayDisplay.classList.add('transition-all')
onErrorDayDisplay.style.display = 'block'
onErrorClrDisplayDay.classList.add('error-clr-display');
daysInput.style.borderColor = '#FF5959';
} else if (userDayInput.trim() === '' && userMonthInput.trim() === '' && userYearInput.trim() === '') {
isError = true
onErrorYearDisplay.classList.add('transition-all')
onErrorYearDisplay.style.display = 'block'
onErrorClrDisplayYear.classList.add('error-clr-display');
yearsInput.style.borderColor = '#FF5959';
onErrorMonthDisplay.classList.add('transition-all')
onErrorMonthDisplay.style.display = 'block'
onErrorClrDisplayMonth.classList.add('error-clr-display');
monthInput.style.borderColor = '#FF5959';
onErrorDayDisplay.classList.add('transition-all')
onErrorDayDisplay.style.display = 'block'
onErrorClrDisplayDay.classList.add('error-clr-display');
daysInput.style.borderColor = '#FF5959';
}
else {
isError = false
//removing all errors styles if iserror is set to false
onErrorYearDisplay.classList.remove('transition-all')
onErrorYearDisplay.style.display = 'none'
onErrorClrDisplayYear.classList.remove('error-clr-display');
yearsInput.style.borderColor = '';
onErrorMonthDisplay.classList.remove('transition-all')
onErrorMonthDisplay.style.display = 'none'
onErrorClrDisplayMonth.classList.remove('error-clr-display');
monthInput.style.borderColor = '';
onErrorDayDisplay.classList.remove('transition-all')
onErrorDayDisplay.style.display = 'none'
onErrorClrDisplayDay.classList.remove('error-clr-display');
daysInput.style.borderColor = '';
//calculations starts here
let ageInYears = currentDate.getFullYear() - userYearInput;
let ageInMonths = currentDate.getMonth() - userMonthInput;
let ageInDays = currentDate.getDate() - userDayInput;
// Adjust for cases where the birth date hasn't occurred yet in the current year
if (currentDate.getMonth() < userMonthInput || (currentDate.getMonth() === userMonthInput && currentDate.getDate() < userDayInput)) {
ageInYears--;
ageInMonths += 12;
}
// Adjust for cases where the birth day hasn't occurred yet in the current month
if (currentDate.getDate() < userDayInput) {
ageInMonths--;
const lastMonthDays = monthsWithDays[(currentDate.getMonth() + 11) % 12]; // Get days of the previous month
ageInDays += lastMonthDays;
}
if (ageInMonths < 0) {
ageInMonths = 0
}
if (ageInMonths > 1) {
ageInMonths += 1
}
if (ageInYears < 1) {
ageInYears = 0
}
// Update this part to include the animation
animateNumbers(ageInYears, ageInMonths, ageInDays);
resolve();
}
})
}
async function animateNumbers(years, months, days) {
// Add the fade-in class to trigger the animation
yearAge.classList.add('fade-in');
monthAge.classList.add('fade-in');
dayAge.classList.add('fade-in');
// Update the text content with the calculated values
yearAge.textContent = years;
monthAge.textContent = months;
dayAge.textContent = days;
// Remove the fade-in class to reset for the next animation
await new Promise(resolve => setTimeout(resolve, 600)); // Using async/await for better readability
yearAge.classList.remove('fade-in');
monthAge.classList.remove('fade-in');
dayAge.classList.remove('fade-in');
}
//event btn listner to check age
datesSubmitBtn.addEventListener('click', function () {
// User display
if (isError === false) {
calculateAge()
} else {
console.log('it seems an error happened while getting the date of birth');
}
});