Skip to content

Commit b296466

Browse files
committed
Add functionalities
1 parent 269e827 commit b296466

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Source-Code/AgeCalculator/script.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
const currDate = document.getElementById("currDate");
3+
const dateOfBirth = document.querySelector("#DOB");
4+
const calcAgeButton = document.getElementById("CalcAge");
5+
const displayAge = document.getElementById("displayAge");
6+
const ageText = document.getElementById("age");
7+
const today = new Date();
8+
9+
currDate.innerText = `Today's Date is: ${today.toLocaleDateString("en-US")}`;
10+
11+
calcAgeButton.addEventListener("click", () => {
12+
const birthDate = new Date(dateOfBirth.value);
13+
let age = today.getFullYear() - birthDate.getFullYear();
14+
const monthDifference = today.getMonth() - birthDate.getMonth();
15+
16+
if (
17+
monthDifference < 0 ||
18+
(monthDifference === 0 && today.getDate() < birthDate.getDate())
19+
) {
20+
age--;
21+
}
22+
23+
displayAge.style.visibility = "visible";
24+
ageText.innerText = `You are ${age} years old.`;
25+
});
26+
});

0 commit comments

Comments
 (0)