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 app using JavaScript

Feng 280

@Feng-09

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


Any tips on how to shorten or more efficiently write my code would be appreciated

Community feedback

Gielth 140

@Gielth

Posted

Hi man, nice code, just a few notes to help you out making it better...

In your js file on line 75, where you used "if (Boolean(day) === false) { document.getElementById("req1").innerHTML = "This field is required"; regday.classList.add("required"); dayInput.classList.add("required"); }"

you can continue this line of code, with what you used on line 93

"if (day > maxDay) { document.getElementById("req1").innerHTML = "Must be a valid day"; dayInput.classList.add("required"); regday.classList.add("required"); day = false; }"

by joining then with if { } else if { } like this:

"if (Boolean(day) === false) { document.getElementById("req1").innerHTML = "This field is required"; regday.classList.add("required"); dayInput.classList.add("required"); } else if (day > maxDay) { document.getElementById("req1").innerHTML = "Must be a valid day"; dayInput.classList.add("required"); regday.classList.add("required"); day = false; }"

This way, your code does have and incredible amount of if's that need to be checked, cause the subsequentials "if" only run in the case of the previous argument check being false...

To further improve it you could make this check in a function that uses the variable you're looking, such as day or month, and maxDay and maxMonth...

I recomend that you study a bit on functions, functions uses and functions properties such as the use of "return value" this way you can eliminate some codes like the btn.removeEventListener... that i don't believe it's needed in this...

Here you can read a bit for the functions and their uses:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

hope this help you out in your coding...

Marked as helpful

1

Feng 280

@Feng-09

Posted

@Gielth thanks a lot. I didn't even think about that else if, but I understand. I'll read more on functions too, thanks

0

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