Sleep Debt Calculator

Hey team! I just posted this on a separate forum not realizing one was open about this project. I am running into the same error and although I’ve looked through the responses here, I can’t quite figure it out.

In regards to the Sleep Debt Calculator ( ) project in the Intro to JavaScript, I can’t seem to debug my code to get it to work. I feel I am overlooking the error and it may be right under my nose, so hoping to get a second pair of eyes on it. I have gone through the step-by-step video as well.

The error keeps coming up as actualSleepHours is not defined (I’ve attached a screenshot). I am trying to rack my brain as to where and how else I should be defining it. See below for my code. If you see anything else that could use cleaning up (spacing is a bit warped pasting into the forum) or where there are errors, please don’t hesitate to let me know!:

const getSleepHours = day => { 
 if (day ==='Sunday') {return 8; 
 } else if (day === 'Monday') {
   return 6;} 
   else if (day === 'Tuesday') {
     return 6;} 
     else if (day === 'Wednesday') {
       return 7;} 
       else if (day === 'Thursday') {
         return 8;} 
         else if (day === 'Friday') {
           return 9;} 
           else if (day === 'Saturday') {return 6;}
           else {return 'Please enter a valid day of the week.';}

const getActualSleepHours = () => 
 getSleepHours('Sunday') +
 getSleepHours('Monday') +
 getSleepHours('Tuesday') +
 getSleepHours('Wednesday') +
 getSleepHours('Thursday') +
 getSleepHours('Friday') +

 const getIdealSleepHours = () => {
  const idealHours = 8;
  return idealHours * 7;

const calculateSleepDebt = () => {
  const actualSleepHours =
  const idealSleepHours =

if(actualSleepHours === idealSleepHours)
  console.log('Congrats! You are getting ' + actualSleepHours + ' hours of sleep per week which is the perfect amount.');
} else if (actualSleepHours > 
idealSleepHours) {
  console.log('Congrats! You are getting ' + (actualSleepHours - idealSleepHours) + ' more hours than the ideal amount of sleep per week.');
} else {console.log('Looks like you need more rest! You are getting ' + (idealSleepHours - actualSleepHours) + ' less hours than the ideal amount of sleep per week.');}


Aside, keep assignments on the same line.

  const actualSleepHours = getActualSleepHours();

Now consider how actualSleepHours is a local variable, so not accessible to your if statement that follows the function.

Thanks for a great play by play account of this series of errors. Your description is dead on. Lesson to myself is to be less careless from the start. Had I not left out Sunday from the onset I believe none of these knock on issues would have come on. I’m much wiser for it now.

The reason I wanted you to try each day separately was because your original code (posted below) did not include ‘Sunday’. That’s why when you ran your code you received NaN as the final output.
Your getActualSleepHours function was returning 8 + 7 + 8 + 8 + 5 + 8 + undefined which evaluates to NaN. Since ‘Sunday’ was missing, your code logged error to the console, and implicitly returned undefined. When you changed your code to return 0; instead of console.log('error'), you got 0 in place of undefined when you tried to get the hours for ‘Sunday’. At some point, you obviously noticed that ‘Sunday’ was missing, and added it. :slightly_smiling_face:

1 Like