Design comparison
Solution retrospective
Hello everyone!
Long time no see! After 2 months break (exams) I'm back even tho I was thinking about qutting it because of the long not-studying/not-repeating.
Here is my solution of age calculator app. I encountered many seemingly basic problems (😭) so I will appreciate feedbacks :)
- Why
calculateBtn.addEventListener('click', isDateValid(daysAgo.value, monthsAgo.value))
doesn't work? Function runs before clicking button. - The way of counting days is messed up in some cases and once works once not.
- Validation worked but now I really don't know what I missed - it doesn't work at all.
Have a great day and happy coding!
Community feedback
- @bramizdevPosted over 1 year ago
Your JS function is not working as expected because you are calling
isDateValid()
immediately and passing its return value as the event listener callback.A quick solution would be using an anonymous function as the event listener callback:
calculateBtn.addEventListener('click', ()=>isDateValid(daysAgo.value, monthsAgo.value))
By the way your approach would have worked if
isDateValid
wouldn't need any argumentscalculateBtn.addEventListener('click', isDateValid)
, When you use parentheses()
after a function name, it immediately executes the function. That's why you need an anonymous function.Marked as helpful0
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