actualSleepHours not defined

I’m getting ReferenceError: actualSleepHours is not defined, I’ve rechecked my code and even compared it to the video help but I still can’t figure it out.

const getSleepHours = day => {
  switch(day) {
    case 'monday':
      return 8
      break;
    case 'tuesday':
      return 4
      break;
    case 'wednesday':
      return 9
      break;
    case 'thursday':
      return 7
      break;
    case 'friday':
      return 5
      break;
    case 'saturday':
      return 9
      break;
    case 'sunday':
      return 4
      break;
    default:
      return 'Error'
      break;
  }
};

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

console.log(getSleepHours('monday'));
console.log(getActualSleepHours());

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

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

if (actualSleepHours === idealSleepHours){
  console.log('perfect');
} else if (actualSleepHours > idealSleepHours){
  console.log('too much');
} else {
  console.log('rest');
};

calculateSleepDebt();

If anyone could help me with this problem, it would be very much appreciated!

1 Like

As we see, that variable is in function scope, and cannot be seen outside of the function body.

Did you intend for the conditional to be inside that same function? Looks like it.

I can’t believe I didn’t notice that, it’s working now. Thank you!

1 Like

Hi, I’m having this same problem with this error message:

if (actualSleepHours === idealSleepHours) {
^

ReferenceError: actualSleepHours is not defined
at Object.

My code is:

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

};

When you say the variable is in function scope, how do you get it outside of the function body? I’ve tried using return to call it, but no luck

Thanks!

1 Like

Hi, I have the same problem. How did you fix it?
Thanks.

Is that all you’ve got? Begging for answers when the answer is only two or three posts above? Surely you have more determination than that. Programmers are brimming with it so prepare to pull up your boot straps.

1 Like

Okay. I was wanting some easy help. I’ve worked on the solving the problem for several hours. I’ll go back and figure out what you mean by “As we see, that variable is in function scope” and I think I know what “outside the function body”. That makes sense to me.
Thanks for replying.
CharlesB60

1 Like

A common mistake is to attempt to access a variable that is in function scope from outside of the function body. Variables defined inside a function are not accessible to the outside.

Thanks, I got it. I went over what I thought I knew and found the culprit. Like you said I was not inside the function body.

1 Like

Thanks is just a matter of {}. :wink:

1 Like