Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Age calculator using javascript

MaryUba 40

@MaryUba

Desktop design screenshot for the Age calculator app coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


I am a bit confused on my Javascript, and my output disappears immediately it shows. Any feedback would be appreciated

Community feedback

Mcnafaha 240

@TheMcnafaha

Posted

Hello@MaryUba,

Your solution looks awesome and the logic of your app is easy to follow.

To answer your question, anytime a form is "submitted" it triggers a page refresh by default. In your app, this happens when the button is clicked (the one with the aptly named CSS class of "submit-btn"). The good news is that this is super easy to fix. Simply add the code I've attached below to your form tag:

   <form onsubmit="event.preventDefault()">

Alternately, you could do the same thing inside your script.js file in case you want to keep all js in one place:

const element = document.querySelector('form');
element.addEventListener('submit', event => {event.preventDefault()});

Both methods will stop the page from being reloaded when the form is submitted. I personally tend to go for the first one but it all comes down to personal taste.

I'm not sure which part of your JavaScript confuses you, but if I had to guess I would say it's your CalculateDate function. If that is what confuses you then I'm sorry to be the bringer of bad news: working with time confuses all programmers. My only two pieces of advice here are the following:

One, always remember that months in JavaScript are 0-indexed (fancy way of saying they start at 0). What this basically means is that if you were born in November 22, 2003, your JS DOB is (yyyy/mm/dd) 2003/10/22. In JS, you have to always subtract one month. Yes, this is very annoying to keep track of and the root of many bugs.

Two, look for existing libraries. Unless you want a challenge, there's a ton of useful time-related libraries that have been literally time tested.

Overall, I really like your code. It shows that you understand HTML and that you know how to use the DOM. I especially liked you use of "isValid" to keep track of all the possible checks the form had to pass before it could be rendered.

Best of wishes, @TheMcnafaha.

Marked as helpful

1

MaryUba 40

@MaryUba

Posted

@TheMcnafaha Thank you so much for taking time to review my code, your feedback was extremely helpful. Yes, my confusion was with the CalculateDate function, your insight on the month was helpful (I've made correction) but I'm still trying to work on the day as it seems to be conflicting. Thanks.

1

@MelvinAguilar

Posted

Hello there 👋. Good job on completing the challenge !

  • By default, a form submission will refresh the page, which might cause your output to disappear. To prevent the form from submitting and refreshing the page, you should add event.preventDefault() at the beginning of the CalculateDate function.

Happy coding!

Marked as helpful

1

MaryUba 40

@MaryUba

Posted

@MelvinAguilar thank you so much for taking time to review my code. Your feedback was helpful, I appreciate. I've immediately corrected it.

1

Please log in to post a comment

Log in with GitHub
Discord logo

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