Sleep Debt Calculator Project Error Message - "ReferenceError: actualSleepHours is not defined"

I can’t seem to figure out why I keep getting “ReferenceError: actualSleepHours is not defined”.

Can you guys please take a look at my code and tell me what I’m doing wrong? I have been banging my head about this for the past few days.

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

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

const getIdealSleepHours = () => {
  let idealHours = 8;
  return idealHours * 7;
}
 
const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours(8);
}

if(actualSleepHours === idealSleepHours) {
  console.log("You've got the perfect amount of sleep!");
  } else if(actualSleepHours > idealSleepHours) {
    console.log("You've got more sleep than needed.");
  } else if(actualSleepHours < idealSleepHours) {
    console.log("You should get some rest!");
  } else {
    console.log("Error! Something went wrong, check your code.");
}


calculateSleepDebt();
`type or paste code here`

Welcome to the forums :tada:

It has to do with scope. In this code you’re only declaring actualSleepHours internally within the calculateSleepDebt() function, it’s not going to carry over elsewhere.

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

So what should I do instead? Take out const actualSleepHours = getActualSleepHours(); from the function it’s in and place it outside the function?

Well there might be multiple ways about it, but an easy one is you can return values from your calculateSleepDebt(). Right now it’s not actually doing anything.

More importantly though I’d lay out on paper where your variables and functions are. That way you know what’s missing and what needs to be added.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.