dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.3 KiB
2.3 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a2037a68a0bc2aef0006003 | Challenge 341: Birthday Countdown | 28 | challenge-341 |
--description--
Given today's date and a birthday, return the number of days until the person's next birthday.
- Today's date is given as a string in
"YYYY-MM-DD"format, with leading zeros, for example:"2026-07-16". - The birthday is given as a string in
"M/D"format, without leading zeros, for example:"9/7". - If today is their birthday, return the number of days until their next birthday (not
0). - Leap years should be accounted for.
--hints--
daysUntilBirthday("2026-07-16", "9/7") should return 53.
assert.equal(daysUntilBirthday("2026-07-16", "9/7"), 53);
daysUntilBirthday("2026-07-16", "3/22") should return 249.
assert.equal(daysUntilBirthday("2026-07-16", "3/22"), 249);
daysUntilBirthday("2026-07-16", "7/16") should return 365.
assert.equal(daysUntilBirthday("2026-07-16", "7/16"), 365);
daysUntilBirthday("2024-02-28", "3/1") should return 2.
assert.equal(daysUntilBirthday("2024-02-28", "3/1"), 2);
daysUntilBirthday("2023-04-24", "12/30") should return 250.
assert.equal(daysUntilBirthday("2023-04-24", "12/30"), 250);
daysUntilBirthday("2024-03-01", "2/29") should return 1460.
assert.equal(daysUntilBirthday("2024-03-01", "2/29"), 1460);
daysUntilBirthday("2096-03-01", "2/29") should return 2920.
assert.equal(daysUntilBirthday("2096-03-01", "2/29"), 2920);
--seed--
--seed-contents--
function daysUntilBirthday(today, birthday) {
return today;
}
--solutions--
function daysUntilBirthday(today, birthday) {
const [year, month, day] = today.split("-").map(Number);
const [bMonth, bDay] = birthday.split("/").map(Number);
const todayDate = new Date(year, month - 1, day);
function isLeapYear(y) {
return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0;
}
function nextValidBirthday(fromYear) {
for (let y = fromYear; y <= fromYear + 8; y++) {
if (bMonth === 2 && bDay === 29 && !isLeapYear(y)) continue;
const date = new Date(y, bMonth - 1, bDay);
if (date > todayDate) return date;
}
}
const nextBirthday = nextValidBirthday(year);
const diff = nextBirthday - todayDate;
return Math.round(diff / (1000 * 60 * 60 * 24));
}