Files
freeCodeCamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69f35a5bb823ed620fcb7cbb.md
T

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);
}