Hello, how do I validate the age calculator in my code:
Calculate Age<input type="text" id="firstname" placeholder="First Name">
<input type="text" id="dob" placeholder="Date of Birth">
<!-- Button to start -->
<button type="button" id="calculateClicked">
Calculate
</button>
<!-- Below is where the result is displayed -->
<div id="userAge"></div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script>
$( () => {
$("#calculateClicked").click( calculateAge );
} );
function calculateAge(){
// Get number of milliseconds in a year
// DaysInYear * HoursInDay * MinutesInHour * SecondsInMinute * MillisecondsInSecond
// Could be "HardCoded" to 31536000000
const millisecondsInAYear = 365.2423 * 24 * 60 * 60 * 1000;
let firstname = $("#firstname").val();
let dob = $("#dob").val();
// Get Difference between Start Date and now
var ageInMilliseconds = (new Date() - Date.parse(dob));
// Get Age in years by dividing ageInMilliseconds by millisecondsInAYear
var ageInYears = ageInMilliseconds / millisecondsInAYear;
// Display result by using document object to
$("#userAge").text(`${firstname} is ${ageInYears} years old.` );
}
</script>