feat: catch up on fcc dailies
Node.js CI / Lint and Test (push) Has been cancelled

This commit is contained in:
2025-11-27 14:33:54 -08:00
parent a50c19990f
commit 6bd6acdfce
6 changed files with 380 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
/**
* @copyright NHCarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/**
* Given the date of someone's birthday in the format YYYY-MM-DD, return the person's age as of November 27th, 2025.
* @param birthday - The date of someone's birthday in the format YYYY-MM-DD.
* @returns The person's age as of November 27th, 2025.
* @throws {TypeError} If the birthday date is invalid.
* @see https://www.freecodecamp.org/learn/daily-coding-challenge/2025-11-27
*/
export const calculateAge = (birthday: string): number => {
const [ year, month, day ] = birthday.split("-").map(Number);
if (
year === undefined
|| month === undefined
|| day === undefined
|| Number.isNaN(year)
|| Number.isNaN(month)
|| Number.isNaN(day)
) {
throw new TypeError("Invalid birthday date");
}
const parsed = new Date(`${birthday}T00:00:00Z`);
if (
Number.isNaN(parsed.getTime())
|| parsed.getUTCFullYear() !== year
|| parsed.getUTCMonth() + 1 !== month
|| parsed.getUTCDate() !== day
) {
throw new TypeError("Invalid birthday date");
}
const referenceYear = 2025;
const referenceMonth = 11;
const referenceDay = 27;
const isLaterMonth = month > referenceMonth;
const isSameMonth = month === referenceMonth;
const isLaterDay = day > referenceDay;
const isLaterDayInSameMonth = isSameMonth && isLaterDay;
const hasUpcomingBirthday = isLaterMonth || isLaterDayInSameMonth;
const birthdayAdjustment = hasUpcomingBirthday
? 1
: 0;
return referenceYear - year - birthdayAdjustment;
};