ActualSleepHours is not defined at Object

Hello, I’m working on this link:

I’m getting this error message below. Please help! Thank you!

7
47
56
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:55
if(actualSleepHours === idealSleepHours){
^

ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:55:4)
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

1 Like

Hi welcome to the forums :slight_smile:

The scopre of your variable actualSleepHours is confined locally to the one function where it’s declared: calculateSleepDebt() .

If you want to use it as you are, you have to declare it first.

function someFunction(){
   let x = 45;
   return x;
}
// you still can't reference x outside of this function
let x = somethingElse;
console.log(x);
//output: somethingElse


If you call x before declaring it you will get the reference error:

function someFunction(){
   let x = 45;
   return x;
}
console.log(x);
//Uncaught ReferenceError: x is not defined
1 Like

What I think you are trying to do in that line of code is call the function getActualSleepHours() and the function getIdealSleepHours() in order to return numbers which will be compared in the conditional part of your “if” statement. You need brackets after the function names. I think that should read

if (getActualSleepHours() === getIdealSleepHours()) {

The easier way to do it without rewriting a bunch of your existing code would be to declare the two variables at the beginning of the function before you start the conditional statements, just like you do at the beginning of calculateSleepDebt() - although, it looks as though you’ve got an extra closing curly bracket right after the two variable declarations in that function that is also going to prevent things from working properly.

1 Like

Thank you very much! So I pretty much declared the variables before the if statements and it works. Thank you!

1 Like