Skip to content

Latest commit

 

History

History
85 lines (65 loc) · 2.67 KB

main.js.md

File metadata and controls

85 lines (65 loc) · 2.67 KB

calculateAge Function

The calculateAge function performs the following tasks:

  1. Get User Input:
    • Retrieves the user's birthdate from an HTML input element with the id 'birthdate'.
let userDate = document.getElementById('birthdate').value;
  1. Check Validity of User Input:
    • Checks if the entered value is not a number (NaN) or an empty string. If invalid, it displays an alert and stops further execution.
if (userDate == NaN || userDate == '') {
  return alert('Enter a valid date of birth. You may have not entered a complete date or entered a non-existent date information.');
}
  1. Create Date Objects:
    • Converts the user's birthdate and the current date into Date objects.
const birthdate = new Date(userDate);
const currentDate = new Date();
  1. Reset Result and Error Display:
    • Hides the result and error elements on the HTML page.
document.getElementById('result').style.display = 'none';
document.getElementById('error').style.display = 'none';
  1. Check Birthdate Validity:
    • If the birthdate is greater than the current date, it displays an error message.
if (birthdate > currentDate) {
  document.getElementById('error').style.display = 'block';
  document.getElementById('error').innerHTML = 'Please enter a valid birthdate.';
  return;
}
  1. Calculate Age Difference:
    • Calculates the age difference between the birthdate and the current date in various units (milliseconds, years, months, weeks, days, hours, minutes, seconds).
const ageInMilliseconds = currentDate - birthdate;
const millisecondsInYear = 1000 * 60 * 60 * 24 * 365.25;
const millisecondsInMonth = millisecondsInYear / 12;
const millisecondsInWeek = 7 * 24 * 60 * 60 * 1000;
// ... (similar calculations for other time units)
  1. Display Age Information:
    • Updates the HTML elements with the calculated age values.
document.getElementById('years').textContent = years;
document.getElementById('months').textContent = months;
// ... (similar updates for other time units)
  1. Calculate and Display Life Milestones:
    • Calculates and displays life milestones like retirement age, half-life, and specific ages.
const retirementAge = 65;
const halfLife = years / 2;
const age18 = 18;
const age21 = 21;
const age30 = 30;

document.getElementById('retirementAge').textContent = retirementAge;
document.getElementById('halfLife').textContent = halfLife.toFixed(2); // Show with two decimal places
// ... (similar updates for other milestones)
  1. Additional Notes:
    • The code provides flexibility to add more milestones as needed.