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.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69f35a5bb823ed620fcb7cbb Challenge 282: Sleep Debt 28 challenge-282

--description--

Given an array of hours slept each night leading up to today, and a target number of hours per night, return how many hours of sleep you need tonight to eliminate your sleep debt.

  • Include tonight's hours in the total time needed to catch up.
  • If you've slept enough to cover tonight's target or more, return 0.

--hints--

sleepDebt([6, 6, 6, 6, 6, 6], 8) should return 20.

assert.equal(sleepDebt([6, 6, 6, 6, 6, 6], 8), 20);

sleepDebt([6, 7, 8, 4, 8, 6], 7) should return 10.

assert.equal(sleepDebt([6, 7, 8, 4, 8, 6], 7), 10);

sleepDebt([10, 10, 9, 10, 9, 11], 9) should return 4.

assert.equal(sleepDebt([10, 10, 9, 10, 9, 11], 9), 4);

sleepDebt([8, 7, 6, 7, 6, 8], 6) should return 0.

assert.equal(sleepDebt([8, 7, 6, 7, 6, 8], 6), 0);

sleepDebt([8, 9, 10, 9, 10, 7], 7) should return 0.

assert.equal(sleepDebt([8, 9, 10, 9, 10, 7], 7), 0);

--seed--

--seed-contents--

function sleepDebt(hoursSlept, targetHours) {

  return hoursSlept;
}

--solutions--

function sleepDebt(hoursSlept, targetHours) {
  const debt = (hoursSlept.length + 1) * targetHours - hoursSlept.reduce((sum, h) => sum + h, 0);
  return Math.max(0, debt);
}