-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
198 lines (163 loc) · 5.44 KB
/
index.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
const form = document.querySelector("form");
const dayLabelElement = document.querySelector("label[for='day']");
const monthLabelElement = document.querySelector("label[for='month']");
const yearLabelElement = document.querySelector("label[for='year']");
const dayInputElement = document.querySelector("input#day");
const monthInputElement = document.querySelector("input#month");
const yearInputElement = document.querySelector("input#year");
const dayErrorElement = document.querySelector("input#day + span");
const monthErrorElement = document.querySelector("input#month + span");
const yearErrorElement = document.querySelector("input#year + span");
form.addEventListener("submit", (event) => {
event.preventDefault();
const formData = getFormData();
const formErrors = getFormErrors(formData);
const hasErrors = Object.keys(formErrors).length !== 0;
if (hasErrors) {
setHtmlErrors(formErrors);
setResults();
return;
}
setHtmlErrors();
const results = calculateAge(formData);
setResults(results);
});
function getFormData() {
const formData = new FormData(form);
const formEntries = Object.fromEntries(formData);
return {
day: formEntries.day.trim(),
month: formEntries.month.trim(),
year: formEntries.year.trim(),
};
}
function getFormErrors({ day, month, year }) {
const errors = {};
if (day === "") {
errors.day = "This field is required";
} else {
const isValidDay = isValidInRange(day, { min: 1, max: 31 });
if (!isValidDay) {
errors.day = "Must be a valid day";
}
}
if (month === "") {
errors.month = "This field is required";
} else {
const isValidMonth = isValidInRange(month, { min: 1, max: 12 });
if (!isValidMonth) {
errors.month = "Must be a valid month";
}
}
if (year === "") {
errors.year = "This field is required";
} else {
const currentDate = new Date();
const currentYear = currentDate.getFullYear();
const isValidYear = isValidInRange(year, { min: 1, max: currentYear });
if (!isValidYear) {
errors.year = "Must be in the past";
}
}
if (Object.keys(errors).length === 0) {
const daysInMonth = getDaysInMonth(year, month - 1);
const isValidDate = isValidInRange(day, { max: daysInMonth });
if (!isValidDate) {
return {
year: "",
month: "",
day: "Must be a valid date",
};
}
}
return errors;
}
function isValidInRange(value, { min, max }) {
const integer = parseInt(value);
if (isNaN(integer)) return false;
if (min && integer < min) return false;
if (max && integer > max) return false;
return true;
}
function setHtmlErrors(errors = {}) {
if (errors.day !== undefined) {
dayLabelElement.classList.add("error");
dayInputElement.classList.add("error");
} else {
dayLabelElement.classList.remove("error");
dayInputElement.classList.remove("error");
}
if (errors.month !== undefined) {
monthLabelElement.classList.add("error");
monthInputElement.classList.add("error");
} else {
monthLabelElement.classList.remove("error");
monthInputElement.classList.remove("error");
}
if (errors.year !== undefined) {
yearLabelElement.classList.add("error");
yearInputElement.classList.add("error");
} else {
yearLabelElement.classList.remove("error");
yearInputElement.classList.remove("error");
}
dayErrorElement.textContent = errors.day;
monthErrorElement.textContent = errors.month;
yearErrorElement.textContent = errors.year;
}
function calculateAge(formData) {
const currentDate = new Date();
const formDataMonth = formData.month - 1; // month is 0-indexed
const formDate = new Date(formData.year, formDataMonth, formData.day);
let yearDifference = currentDate.getFullYear() - formDate.getFullYear();
let monthDifference = currentDate.getMonth() - formDate.getMonth();
if (monthDifference < 0) {
monthDifference += 12;
yearDifference -= 1;
}
let dayDifference = currentDate.getDate() - formDate.getDate();
if (dayDifference < 0) {
const daysInMonth = getDaysInMonth(formData.year, formDataMonth);
dayDifference += daysInMonth;
monthDifference -= 1;
if (monthDifference < 0) {
monthDifference += 12;
yearDifference -= 1;
}
}
return {
day: dayDifference,
month: monthDifference,
year: yearDifference,
};
}
function getDaysInMonth(year, month) {
// Create a new date object for the first day of the next month
const date = new Date(year, month + 1, 0);
console.log(date);
// Get the day of the month from the new date object, which gives us the number of days in the month
const daysInMonth = date.getDate();
return daysInMonth;
}
function setResults(
{ day, month, year } = { day: "--", month: "--", year: "--" }
) {
const resultsDiv = document.querySelector(".result");
// Remove children from node
[...resultsDiv.children].forEach((child) => {
resultsDiv.removeChild(child);
});
// Add children to node
resultsDiv.appendChild(createResultNode(year, "year"));
resultsDiv.appendChild(createResultNode(month, "month"));
resultsDiv.appendChild(createResultNode(day, "day"));
}
function createResultNode(value, name) {
const parentNode = document.createElement("p");
const valueNode = document.createElement("span");
valueNode.textContent = value;
const textNode = document.createTextNode(` ${name}${value === 1 ? "" : "s"}`);
parentNode.appendChild(valueNode);
parentNode.appendChild(textNode);
return parentNode;
}