Design comparison
Solution retrospective
Hello hackers,
This is my solution to the age calculator challenge.
I have the following issues, if i click submit when the input forms are empty, I see the error message and then if I add some values in the input and reclick submit the app calculates a false date. I tried to understand why does it happen but I still can't get it.
Then I saw that the app works one time then I have to refresh the page again to make it work. I suppose that I need to use a load or ready function?
Any suggestions, tips are really welcome
Thanks in advance :) Kind regards Eli
Community feedback
- @3eze3Posted about 1 year ago
Hi Elisabeth Erkekoglou good job with your code I have some recommendations regarding the logic:
JavaScript:
- Answering your question that you only run your program once, you don't need any other function , you just have a bug in how you handle events and how you get the inputs when clicking.
Let's see your code:
/*inputs values*/ let day = inputDay.value; let month = inputMonth.value; let year = inputYear.value; /*Current Date*/ let currentDate = new Date(); let currentYear = currentDate.getFullYear(); let currentMonth = currentDate.getMonth() + 1; let currentDay = currentDate.getDate(); /*Results*/ let years = currentYear - year; let months = currentMonth - month; let days = currentDay - day;
The first thing is that you are taking the value of the inputs when there is no value, for that reason that you get the current date always and does not change even if we enter values.
To obtain the values of the inputs we do it at the moment of giving click and not before since they do not have value, this way we can work with these and to make the respective calculations.
And also I see a lot of duplication in your code, you could simplify with functions that only handle single responsibilities or modularize by classes, to have less problems in the future.
I can't see in depth your code, but I hope this is useful for you. And if you don't understand something feel free to write me. Happy coding.🍟
1
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