-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
18 lines (15 loc) · 724 Bytes
/
script.js
File metadata and controls
18 lines (15 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function calculateAge() {
const birthDate = document.getElementById('birthDate').value;
if (birthDate) {
const birthDateObj = new Date(birthDate);
const currentDate = new Date();
let age = currentDate.getFullYear() - birthDateObj.getFullYear();
const monthDifference = currentDate.getMonth() - birthDateObj.getMonth();
if (monthDifference < 0 || (monthDifference === 0 && currentDate.getDate() < birthDateObj.getDate())) {
age--;
}
document.getElementById('result').textContent = `You are ${age} years old.`;
} else {
document.getElementById('result').textContent = 'Please enter a valid birthdate.';
}
}