Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.5 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69b1028d6e265413d0198a2b Challenge 232: Due Date 28 challenge-232

--description--

Given a date string, return the date 9 months in the future.

  • The given and return strings have the format "YYYY-MM-DD".
  • If the month nine months into the future doesn't contain the original day number, return the last day of that month.

--hints--

getDueDate("2025-03-30") should return "2025-12-30".

assert.equal(getDueDate("2025-03-30"), "2025-12-30");

getDueDate("2025-04-27") should return "2026-01-27".

assert.equal(getDueDate("2025-04-27"), "2026-01-27");

getDueDate("2025-05-29") should return "2026-02-28".

assert.equal(getDueDate("2025-05-29"), "2026-02-28");

getDueDate("2026-06-30") should return "2027-03-30".

assert.equal(getDueDate("2026-06-30"), "2027-03-30");

getDueDate("2026-10-11") should return "2027-07-11".

assert.equal(getDueDate("2026-10-11"), "2027-07-11");

--seed--

--seed-contents--

function getDueDate(dateStr) {

  return dateStr;
}

--solutions--

function getDueDate(dateStr) {
  let [year, month, day] = dateStr.split("-").map(Number);

  month += 9;

  year += Math.floor((month - 1) / 12);
  month = ((month - 1) % 12) + 1;

  const daysInMonth = new Date(year, month, 0).getDate();

  if (day > daysInMonth) {
    day = daysInMonth;
  }

  const mm = String(month).padStart(2, "0");
  const dd = String(day).padStart(2, "0");

  return `${year}-${mm}-${dd}`;
}