|
| 1 | + |
| 2 | +### `calculateAge` Function |
| 3 | + |
| 4 | +The `calculateAge` function performs the following tasks: |
| 5 | + |
| 6 | +1. **Get User Input:** |
| 7 | + - Retrieves the user's birthdate from an HTML input element with the id 'birthdate'. |
| 8 | + |
| 9 | +```javascript |
| 10 | +let userDate = document.getElementById('birthdate').value; |
| 11 | +``` |
| 12 | + |
| 13 | +2. **Check Validity of User Input:** |
| 14 | + - Checks if the entered value is not a number (NaN) or an empty string. If invalid, it displays an alert and stops further execution. |
| 15 | + |
| 16 | +```javascript |
| 17 | +if (userDate == NaN || userDate == '') { |
| 18 | + return alert('Enter a valid date of birth. You may have not entered a complete date or entered a non-existent date information.'); |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +3. **Create Date Objects:** |
| 23 | + - Converts the user's birthdate and the current date into `Date` objects. |
| 24 | + |
| 25 | +```javascript |
| 26 | +const birthdate = new Date(userDate); |
| 27 | +const currentDate = new Date(); |
| 28 | +``` |
| 29 | + |
| 30 | +4. **Reset Result and Error Display:** |
| 31 | + - Hides the result and error elements on the HTML page. |
| 32 | + |
| 33 | +```javascript |
| 34 | +document.getElementById('result').style.display = 'none'; |
| 35 | +document.getElementById('error').style.display = 'none'; |
| 36 | +``` |
| 37 | + |
| 38 | +5. **Check Birthdate Validity:** |
| 39 | + - If the birthdate is greater than the current date, it displays an error message. |
| 40 | + |
| 41 | +```javascript |
| 42 | +if (birthdate > currentDate) { |
| 43 | + document.getElementById('error').style.display = 'block'; |
| 44 | + document.getElementById('error').innerHTML = 'Please enter a valid birthdate.'; |
| 45 | + return; |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +6. **Calculate Age Difference:** |
| 50 | + - Calculates the age difference between the birthdate and the current date in various units (milliseconds, years, months, weeks, days, hours, minutes, seconds). |
| 51 | + |
| 52 | +```javascript |
| 53 | +const ageInMilliseconds = currentDate - birthdate; |
| 54 | +const millisecondsInYear = 1000 * 60 * 60 * 24 * 365.25; |
| 55 | +const millisecondsInMonth = millisecondsInYear / 12; |
| 56 | +const millisecondsInWeek = 7 * 24 * 60 * 60 * 1000; |
| 57 | +// ... (similar calculations for other time units) |
| 58 | +``` |
| 59 | + |
| 60 | +7. **Display Age Information:** |
| 61 | + - Updates the HTML elements with the calculated age values. |
| 62 | + |
| 63 | +```javascript |
| 64 | +document.getElementById('years').textContent = years; |
| 65 | +document.getElementById('months').textContent = months; |
| 66 | +// ... (similar updates for other time units) |
| 67 | +``` |
| 68 | + |
| 69 | +8. **Calculate and Display Life Milestones:** |
| 70 | + - Calculates and displays life milestones like retirement age, half-life, and specific ages. |
| 71 | + |
| 72 | +```javascript |
| 73 | +const retirementAge = 65; |
| 74 | +const halfLife = years / 2; |
| 75 | +const age18 = 18; |
| 76 | +const age21 = 21; |
| 77 | +const age30 = 30; |
| 78 | + |
| 79 | +document.getElementById('retirementAge').textContent = retirementAge; |
| 80 | +document.getElementById('halfLife').textContent = halfLife.toFixed(2); // Show with two decimal places |
| 81 | +// ... (similar updates for other milestones) |
| 82 | +``` |
| 83 | + |
| 84 | +9. **Additional Notes:** |
| 85 | + - The code provides flexibility to add more milestones as needed. |
0 commit comments