I'm stuck on Sleep Debt Calculator Javascript lesson>Step 9

const getSleepHours = day => 
{
  if (day === 'monday'){
    return 8;
  } else if (day === 'tuesday'){
    return 7;
  } else if (day === 'wednesday'){
    return 6; 
  } else if (day === 'thursday'){
    return 8;
  } else if (day === 'friday') {
    return 9; 
  } else if (day === 'saturday') {
    return 6;
  } else if (day === 'sunday') {
    return 8;
  } else {
    return unknown;
  }
};

const getActualSleepHours = () => 
getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + 
getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours ('sunday'); 

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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = 
getIdealSleepHours();
}; 


if (actualSleepHours === idealSleepHours) {
 console.log("You got the perfect amount of sleep")
}  

else if (actualSleepHours > idealSleepHours) {
  console.log('Too much sleep')
} 

else if (actualSleepHours < idealSleepHours) {
  console.log('Get more sleep')
} 

else {
  console.log('Something went wrong, check your code!')
}

if (actualSleepHours < idealSleepHours) {
  console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) less sleep than you needed this week. Get some rest.');
}

else if (actualSleepHours > idealSleepHours) {
  console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hour(s) more sleep than you needed this week');
}

else if (actualSleepHours === idealSleepHours) {
  console.log('You got ' + (actualSleepHours === idealSleepHours) + 'you have just the right amount of sleep you need')
};

calculateSleepDebt();

The if statement here:

uses variables outside of their scope. Consider looking at the code here:

Should you be closing the function?

Also, you don’t need the semicolon after the closing curly brace }.

I hope this helps!

Well, I keep getting error messages elsewhere. I’m over my head. I’m going to erase it, & start all over. I’ll watch the video & see how Galena handles the project. I’ll get back to you. Thank you anyway for your efforts to assist me, I appreciate it. :grinning:

1 Like

I completed the project. Thank you for your help.

1 Like

I’m stuck on this step, too. Here’s the error message:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:44
if (actualSleepHours === idealSleepHours) {
^

ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:44:5)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3

Could you post your full code please? Make sure to format it according to this style guide.

const getSleepHours = day => {
  switch (day) {
    case 'Sunday':
    return 8.00;
    break;
    case 'Monday':
    return 8.00;
    break;
    case 'Tuesday':
    return 8.00;
    break;
    case 'Wednesday':
    return 8.00;
    break;
    case 'Thursday':
    return 8.00;
    break;
    case 'Friday':
    return 8.00;
    break;
    case 'Saturday':
    return 8.00;
    break;
    default:
    return 'Error!';
  }
};
const getActualSleepHours = () =>
  getSleepHours('Sunday')+
  getSleepHours('Monday')+
  getSleepHours('Tuesday')+
  getSleepHours('Wednesday')+
  getSleepHours('Thursday')+
  getSleepHours('Friday')+
  getSleepHours('Saturday');
const getIdealSleepHours = () => {
  let idealHours = 8.00;
  return idealHours * 7;
};
const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
};
if (actualSleepHours === idealSleepHours) {
  console.log('You got just enough sleep!');
} else if (actualSleepHours > idealSleepHours) {
  console.log('You got more sleep than needed.');
} else {
  console.log('You did not get enoguh sleep.');
}

You declared actualSleepHours and idealSleepHours inside the function calculateSleepDebt() which makes them out of reach outside the function scope. Especially since you don’t even call that function.
You don’t need to wrap the assignments of actualSleepHours and idealSleepHours in a function anyway.
Instead wrap that function around your if else statement and call it.

That was from step eight, which the video and hints say is correct.

Ok. Then follow the instructions from step 9 onwards. They tell you what to inside that function with the variables you declared there. The way you did it, the actualSleepHours and idealSleepHours are hidden inside that function scope and out of reach for your conditions.

Okay, how’s this? I placed the “If/Else” statement inside the declaration of getActualSleepHours and getIdealSleepHours:

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours) {
    console.log('You got just enough sleep!');
    } else if (actualSleepHours > idealSleepHours) {
    console.log('You got more sleep than needed.');
  } else {
    console.log('You did not get enough sleep.');
}
};

Yep. That’s what they asked you to do.
This is what I suggested:

const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();

const calculateSleepDebt = ( actualSleepHours, idealSleepHours) => {
  if (actualSleepHours === idealSleepHours) {
    console.log('You got just enough sleep!');
    } else if (actualSleepHours > idealSleepHours) {
    console.log('You got more sleep than needed.');
  } else {
    console.log('You did not get enough sleep.');
}
};

Works too, not sure they would have let you pass with this. Plus, if you don’t need these variables anywhere else, less global scope pollution your way…

2 Likes

Okay. I got the whole code to work. Thanks for your help.

2 Likes

Thanks for your quick response. I made a typo error instead of ‘Thursday’ rather it was ‘Thurdsay’ so was give me NaN as the output but I figure it out.

Just ran into this issue today, and it seems that Codecademy has a serious ongoing issue with not fixing improper methods when they’ve been called out. Both the hints and the project walkthrough video tell us to do this project incorrectly, then we’re left to the forums to decipher it ourselves after the issue has existed for over a year without repair. If it wasn’t for mirja_t I’d still be staring at the screen wondering why the code isn’t running when I did everything in mirror of what was taught.

1 Like

Same here! Actually its correct in the video but you would not notice as a beginner. Sent them a bug report

1 Like

Thank you for your conscience effort to parse out the confusion from the ever-more confusing confusion mirja_t.

I had the same code applied as above from the original poster, step by step, one-for-one. Admittedly, I spent about close to 14 hours over two weeks addressing what exactly it was within my own code that I had…messed up? I really appreciate the in-depth dive and explanation from the quoted blurbs above and below the posters code concerns.

It was quite the learning session, but it turns out that I forgot to capitalize just ONE letter in the actualSleepHours, and for close to two weeks I kept scanning over it to only realize it was spelled out as actualSleephours (notice the “h” not being capital “H” aha). Again, thanks for the quick lesson.

1 Like

I used the code mirja_t used to get the code to work. Thank you. Can’t waste hours trying to figure that out.

Hi Everyone, I’m also stuck in this exercise. Here is the code I wrote. For some reason, I am not able to log/print anything for the calculateSleepDebt(). Your help is greatly appreciated. Thank you very much.

const getSleepHours = day => {
if (day === ‘Monday’) {
return 8;
} else if (day === ‘Tuesday’) {
return 6;
} else if (day === ‘Wednesday’) {
return 7;
} else if (day === ‘Thursday’) {
return 7.5;
} else if (day === ‘Friday’) {
return 6.5;
} else if (day === ‘Saturday’) {
return 7;
} else if (day === ‘Sunday’) {
return 6;
}
};
const getActualSleepHours = () => 6 + 8 + 7 + 6 + 5 + 7 + 8;
const getIdealSleepHours = () => {
const idealHours = 7.5;
return idealHours * 7;
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours(8);
if (actualSleepHours === idealSleeplHours) {
console.log(‘You got perfect amount of sleep.’);
} else if (actualSleepHours > idealSleepHours) {
console.log(‘You got more sleep than you need.’);
} else if (actualSleepHours > idealSleepHours) {
console.log(‘You got’ + (idealSleepHours - actualSleepHours) + ‘hours less sleep than you needed this week. Get some rest.’);
calculateSleepDebt();
};
};
};

You have some of the } in the wrong place.
The way you have the code now,
the calculateSleepDebt function is inside the getIdealSleepHours function.
That should not be the case.

const getSleepHours = day => {
  if (day === 'Monday') {
     return 8;
  } else if (day === 'Tuesday') {
     return 6;
  } else if (day === 'Wednesday') {
     return 7;
  } else if (day === 'Thursday') {
     return 7.5;
  } else if (day === 'Friday') {
     return 6.5;
  } else if (day === 'Saturday') {
     return 7;
  } else if (day === 'Sunday') {
     return 6;
  }
};

const getActualSleepHours = () => 6 + 8 + 7 + 6 + 5 + 7 + 8;

const getIdealSleepHours = () => {
  const idealHours = 7.5;
  return idealHours * 7;
// }; should be here so the function ends here

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours(8);
  if (actualSleepHours === idealSleeplHours) {
     console.log(‘You got perfect amount of sleep.’);
  } else if (actualSleepHours > idealSleepHours) {
     console.log(‘You got more sleep than you need.’);
  } else if (actualSleepHours > idealSleepHours) {
     console.log('You got' + (idealSleepHours - actualSleepHours) + 'hours less sleep than you needed this week. Get some rest.');
  // } should be here to end the else if block
// }; should be here to end the calculateSlepDebt function

calculateSleepDebt(); // this line must be outside all the functions
};  // does not belong here
};  // does not belong here
};  // does not belong here