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

66 lines
1.4 KiB
Markdown

---
id: 69f35a5bb823ed620fcb7cbb
title: "Challenge 282: Sleep Debt"
challengeType: 28
dashedName: 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`.
```js
assert.equal(sleepDebt([6, 6, 6, 6, 6, 6], 8), 20);
```
`sleepDebt([6, 7, 8, 4, 8, 6], 7)` should return `10`.
```js
assert.equal(sleepDebt([6, 7, 8, 4, 8, 6], 7), 10);
```
`sleepDebt([10, 10, 9, 10, 9, 11], 9)` should return `4`.
```js
assert.equal(sleepDebt([10, 10, 9, 10, 9, 11], 9), 4);
```
`sleepDebt([8, 7, 6, 7, 6, 8], 6)` should return `0`.
```js
assert.equal(sleepDebt([8, 7, 6, 7, 6, 8], 6), 0);
```
`sleepDebt([8, 9, 10, 9, 10, 7], 7)` should return `0`.
```js
assert.equal(sleepDebt([8, 9, 10, 9, 10, 7], 7), 0);
```
# --seed--
## --seed-contents--
```js
function sleepDebt(hoursSlept, targetHours) {
return hoursSlept;
}
```
# --solutions--
```js
function sleepDebt(hoursSlept, targetHours) {
const debt = (hoursSlept.length + 1) * targetHours - hoursSlept.reduce((sum, h) => sum + h, 0);
return Math.max(0, debt);
}
```