Sitemap

FreeCodeCamp Daily Challenge Solution 5/19/2026

1 min readMay 19, 2026

--

Sleep Debt (JavaScript)

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.

My Solution (JS):

function sleepDebt(hoursSlept, targetHours) {
// Define neededHours as a single number based on how many hours away from the target each number in the array is plus the target.
let neededHours = hoursSlept.reduce((acc, cur) => {
return acc + (targetHours - cur)
}, targetHours)

// If a positive number of hours is needed, return that number otherwise default to 0.
console.log(neededHours)
return neededHours >= 0
? neededHours
: 0
}

If you have a different solution or an improvement to this one please feel free to comment with your ideas!

Get Riley Underwood’s stories in your inbox

Join Medium for free to get updates from this writer.

See all the solutions I’ve done for these practice problems: https://github.com/BoltenBeeper/FreeCodeCamp-Daily-Coding-Practice-Solutions

Note: I do not use LLMs or AI assistance to write my code. Feel free to use this solution to learn or improve.

Dave Cross also has a great solution on YouTube: https://www.youtube.com/@dave-cross

If you want to see more of me: boltenbeeper.github.io/portfolio

--

--

Riley Underwood
Riley Underwood

Written by Riley Underwood

0 followers

AKA BoltenBeeper - I build websites: boltenbeeper.github.io/portfolio