-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp copy.js
205 lines (179 loc) · 5.29 KB
/
app copy.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
// import validateInput from "./utils/validateInput.js";
// select button
const btn = document.querySelector(".btn");
// select inputs
const inputs = document.querySelectorAll("input");
// select spans
const valuePlaces = document.querySelectorAll("span");
// get values of inputs as an object
const getValuesObject = () => {
const valuesObject = Array.from(inputs).reduce(
(acc, input) => ({
...acc,
[input.id]: input.value,
}),
{}
);
// console.log(valuesObject);
return valuesObject;
};
// function to display the error messages on UI
const displayError = (inputId, text) => {
const input = document.getElementById(inputId);
input.classList.add("border-primaryLightRed");
const label = input.previousElementSibling;
label.classList.add("text-primaryLightRed");
const errorParagraph = input.nextElementSibling;
errorParagraph.textContent = text;
};
const validateInput = (valuesObject) => {
// get the values from the valuesObject object
const { day, month, year } = valuesObject;
// Add unique IDs to each input field
inputs.forEach((input, index) => {
if (index === 0) {
input.id = "day";
} else if (index === 1) {
input.id = "month";
} else if (index === 2) {
input.id = "year";
}
});
// sets the error as false by default
let hasError = false;
// check valid day
if (!day) {
hasError = true;
const text = "This field is required";
displayError("day", text);
} else if (day < 1 || day > 31) {
hasError = true;
const text = "Must be a valid day";
displayError("day", text);
} else if ([4, 6, 9, 11].includes(Number(month)) && day > 30) {
hasError = true;
const text = "Must be a valid day";
displayError("day", text);
} else if (month === 2) {
if (
((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) &&
day > 29
) {
hasError = true;
const text = "Must be a valid day";
displayError("day", text);
} else if (day > 28) {
hasError = true;
const text = "Must be a valid day";
displayError("day", text);
}
}
// check valid month
if (!month) {
hasError = true;
const text = "This field is required";
displayError("month", text);
} else if (month < 1 || month > 12) {
hasError = true;
const text = "Must be a valid month";
displayError("month", text);
}
// check valid year
if (!year) {
hasError = true;
const text = "This field is required";
displayError("year", text);
} else if (year > new Date().getFullYear()) {
hasError = true;
const text = "Must be in the past";
displayError("year", text);
}
// console.log(hasError);
return hasError;
};
const calculateAge = (valuesObject) => {
// get the values from the valuesObject object
const { day, month, year } = valuesObject;
// sets the birthdate as a date object and gets today's date
const birthDate = new Date(Number(year), Number(month) - 1, Number(day));
const todaysDate = new Date();
// Calculates the number of years
const ageInMilliseconds = todaysDate - birthDate;
const millisecondsPerYear = 1000 * 60 * 60 * 24 * 365.25;
let ageYears = Math.floor(ageInMilliseconds / millisecondsPerYear);
let ageMonths = todaysDate.getMonth() - birthDate.getMonth();
let ageDays = todaysDate.getDate() - birthDate.getDate();
if (ageDays < 0) {
ageMonths -= 1;
if ([4, 6, 9, 11].includes(Number(month))) {
ageDays += 30;
} else if (month === 2) {
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
ageDays += 29;
} else {
ageDays += 28;
}
} else {
ageDays += 31;
}
}
if (ageMonths < 0) {
ageMonths += 12;
}
const age = [ageYears, ageMonths, ageDays];
// console.log(age);
// add an id to the valuePlaces corresponding to each age value
valuePlaces.forEach((place, index) => {
place.id = age[index];
});
return age;
};
// Function to display the age values in the UI with an animation
const updateCount = (el) => {
const value = parseInt(el.id);
const increment = Math.ceil(value / 1000);
let initialValue = 0;
const increaseCount = setInterval(() => {
initialValue += increment;
if (initialValue > value) {
el.textContent = value;
clearInterval(increaseCount);
return;
}
el.textContent = initialValue;
}, 1);
// console.log(increaseCount);
};
// function to handle the click of the button
const handleClick = () => {
// Reset input values
inputs.forEach((input) => {
input.classList.remove("border-primaryLightRed");
input.previousElementSibling.classList.remove("text-primaryLightRed");
input.nextElementSibling.textContent = "";
});
// Get input values
const valuesObject = getValuesObject();
// validate input values
let hasError = validateInput(valuesObject);
if (!hasError) {
// Calculate age
const age = calculateAge(valuesObject);
// updates the age values in the UI with animation
valuePlaces.forEach((item) => {
updateCount(item);
});
// Reset input values
inputs.forEach((input) => {
input.value = "";
});
}
};
// event listener on button
btn.addEventListener("click", handleClick);
// event listener on enter key
document.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
btn.click();
}
});