Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 1 year ago

Responsive age calcuator using CSS, HTML, JS

r3cla•40
@r3cla
A solution to the Age calculator app challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


This challenge really exposed my weaknesses, notably Javascript. Nonetheless it was a very good experience and fun (although stressful) to do!


Test for yourself:

  • Live site
  • Repo

After some quick testing, the app does the following:

  • Displays the age for any valid birth date.
  • Displays relevant errors.
  • Animates the age output.

Devices used for testing:

  • Firefox browser (desktop)
  • Firefox browser (mobile)
  • Firefox responsive design mode (Custom 320px width, Android S10, Android S20 and iPad OS 14.7.1)

Note: The page was designed for devices starting with a minimum width of 320px, then scaled up using 2 breakpoints: 640px and 1800px.


The HTML/CSS side was relatively straightforward, but the JS was pretty hectic for me and I admittedly needed to do alot of googling, but here are the condensed steps that led to me completing the script:

Initialization on Page Load: How: Implemented an event listener for DOMContentLoaded to trigger the JavaScript code. This ensures the script interacts with fully loaded elements.

Variable Initialization: How: Selected form elements using document.getElementById and querySelectorAll to create JavaScript references to these HTML elements. This setup allows the script to easily read inputs and display outputs or errors.

Utility Functions: How: Developed custom JavaScript functions. isNumber checks for numeric input using a regular expression, and clearErrorStyles and applyErrorStyles use class manipulation to alter the appearance of form elements, enhancing user feedback.

Age Calculation Logic: How: Created a function that takes birthdate and current date, calculates the difference in years, months, and days. Special attention was given to the intricacies of date calculations, like handling the end of months and leap years, using JavaScript's Date object.

Displaying Calculated Age: How: Utilized DOM manipulation to update the text content of specific span elements. This function dynamically alters the innerHTML of these elements to show the calculated age.

Date Validation Mechanism: How: Built a comprehensive validation function that checks each input field for common errors such as empty values, non-numeric characters, and unrealistic date entries. It utilizes conditional statements to enforce these rules and provide appropriate feedback.

Form Submission Customization: How: Overrode the default form submission behavior by adding an event listener to the form's submit event. This modification calls the validation function, preventing the form from submitting if the date is invalid and proceeding with the age calculation if it is valid.

Useful resources:

  • W3Schools for basic syntax and examples.
  • Stack Overflow for troubleshooting specific issues and community advice.
  • GPT-4 for guidance, code optimization suggestions, and best practices.
  • Frontend Mentor for growing your skillset and the awesome community.

As always, feedback is welcome!

ps. I noticed the solution screenshot is displaying a mobile version of the site and not the desktop one.

Code
Couldn’t fetch repository

Please log in to post a comment

Log in with GitHub

Community feedback

No feedback yet. Be the first to give feedback on r3cla's solution.

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

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.