
Responsive Age calculator app
Design comparison
Solution retrospective
I need help implementing logic to handle invalid dates entered by the user, such as 31/04/1991 (since April has only 30 days).
Community feedback
- P@johnnygerardPosted about 1 month ago
Sorry, I thought you were asking for how to compute the age.
For the date validation, I used this function:
const isNonexistent = (date: Date, year: number, month: number, day: number): boolean => { return date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day; }
Here is a better function:
/** * Validates if the given year, month, and day form a valid date. * * @param {number} year - The year to validate. * @param {number} month - The month to validate (1-12). * @param {number} day - The day to validate. * @returns {boolean} True if the date is valid, otherwise false. */ const isValidDate = (year: number, month: number, day: number): boolean => { const date = new Date(year, month - 1, day); return ( date.getFullYear() === year && date.getMonth() === month - 1 && date.getDate() === day ); };
Marked as helpful1@Yaciine19Posted about 1 month ago@johnnygerard Thank you so much for your help, Johny. I’ve corrected the code, and now it’s much better and works correctly. If you have any other suggestions for improvement, I’d love to hear them.
1 - P@johnnygerardPosted about 1 month ago
Hello Yacine,
I already wrote a detailed explanation for this challenge.
Hopefully, that helps you.
Good day.
Marked as helpful1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord