Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion projects/age-calculator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@ const resultEl = document.getElementById("result");

function calculateAge() {
const birthdayValue = birthdayEl.value;

// 🔥 VALIDATION 1: EMPTY CHECK
if (birthdayValue === "") {
alert("Please enter your birthday");
} else {
return;
}

// 🔥 VALIDATION 2: FUTURE DATE BLOCK
const selectedDate = new Date(birthdayValue);
const today = new Date();

if (selectedDate > today) {
alert("Future date not allowed");
return;
}

// ✅ ORIGINAL CODE
const age = getAge(birthdayValue);
resultEl.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old`;
}
const age = getAge(birthdayValue);
resultEl.innerText = `Your age is ${age} ${age > 1 ? "years" : "year"} old`;
}
Expand Down