Age calculator
React.js
easy
30 mins
Create a responsive Age Calculator component that calculates a user’s age in years, months, and days based on a selected or entered birthdate.
Requirements
- A label
"Enter/Select a birthdate"
indicating the user to either input manually or select a date from the datepicker. - The interface must include:
- An input field with the placeholder of the format (dd-mm-yyyy or mm-dd-yyyy)
- The input should use the browser's built-in datepicker to allow users to manually select day, month, and year.
- A button
"Calculate Age"
- A display area below the button which will show the results or the errors
- Behavior on clicking the Calculate Age button:
- If valid:
- Display the result in the format:
"X years, Y months, Z days "
- Display the result in the format:
- If invalid:
- Display an error message:
- For future dates- "Birthdate cannot be in the future"
- For empty input- "Please select a date"
- Display an error message:
- If valid:
Edge Cases & Constraints
- Input must be a valid past date
- Empty or future dates must show a user-friendly error
- The datepicker must open when clicked
- Age should be calculated using precise difference in years, months, and days
- The calculation must be accurate even across leap years
- Result must update immediately upon clicking "Calculate"
Testing Requirements
Data Test IDs (Required for Testing):
data-testid="input-birthdate"
— Date input fielddata-testid="btn-calculate"
— Calculate age buttondata-testid="age-result"
— Display area for resultdata-testid="error-msg"
— Display area for errorsdatatestid="label-birthdate"
-Label associated with the birthdate input
Reference UI
Preview what you need to build
Companies:
stripe
linkedin
oracle
Solve Similar questions 🔥