Age calculator with vanilla JS

Solution retrospective
I was able to complete the challenge with almost no help except age calc function.
What challenges did you encounter, and how did you overcome them?Calculating exact age so I had to take some help from ChatGPT. Also, the logic isn't fully correct yet so I will polish code more and optimize it with better logic.
What specific areas of your project would you like help with?Feedback would be appreciated.
Please log in to post a comment
Log in with GitHubCommunity feedback
- P@dar-ju
Hi Bhavik!
Great job!
I would add more restrictions to the fields so that they don't enter anything extra. For example, only numbers and no more than 2 digits in the date and month, no more than 4 digits in the year. Take a look at regular expressions, they are perfect in this case. For the date, it would look like this:
const isValid = /^\d{1,2}$/.test(dayInput.value);
/^\d{1,2}$/
- only numbers and 1 or 2 digits `.test - checks the input and returns true or falseThis thing will shorten your code a little and make it a little more reliable.
In the part of the code if (userDate > 29 && userMonth === 2) { This is more of a day error than a month error. The month is specified correctly.
In general, you went the hard way, where you check everything manually. You can check the correctness of the date using
new Date
. This thing will shorten the code several times)On the other hand, it is useful to rack your brain over various tasks, it will come in handy later anyway.
Marked as helpful - @bhavikthakur
Absolutely! You are right to point this out. I'd certainly fix this. I learn by building, actually, so I just watched one video about a date object and implemented it. I went the hard way, probably wrote a few extra lines of code. Thanks for letting me know, I'd wrap my head around date objects and regular expressions now, and try to optimize the code. Appreciate your feedback, Andrey! Keep them coming :))
Join 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