Why is actualSleepHours is not defined? How do I fix it

const getSleepHours = day => {

 switch(day) {

   case 'monday':

   return 8

   break;

   case 'tuesday':

   return 8

   break;

   case 'wednesday':

   return 8

   break;

  case 'thursday':

   return 8

   break;

  case 'friday':

   return 8

   break;

   case 'saturday':

   return 8

   break;

   case 'sunday':

   return 8

   break;

  default:

  return 'error';

 }

}

console.log(getSleepHours('sunday'))

const getActualSleepHours = () =>

getSleepHours('monday') +

 getSleepHours('tuesday') +

  getSleepHours('wednesday') +

   getSleepHours('thursday') +

    getSleepHours('friday') +

     getSleepHours('saturday') +

     getSleepHours('sunday');

console.log(getActualSleepHours())

const getIdealSleepHours = () => {

  let idealHours = 8; 

    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('You got more sleep than needed');

  } else if(**actualSleepHours** < idealSleepHours) {

    console.log('You should sleep more bum');

  } else {

    console.log('Something went wrong!')

  }

if (**actualSleepHours** < idealSleepHours) {

  return console.log('You had' + (idealSleepHours - actualSleepHours) + ' hours less than you should\'ve had.');

}

why is actualSleepHours is not defined!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

the variables are declared within the body of your function:

const calculateSleepDebt = () => {

const actualSleepHours = getActualSleepHours();

const idealSleepHours = getIdealSleepHours();

}

But that is all your function does, all the conditional clauses are outside/after the function for some reason. Why?

Im not sure to be honest, haven’t learned enough about javascript to know why on that part. So is
const calculateSleepDebt = () => {

const actualSleepHours = getActualSleepHours();

const idealSleepHours = getIdealSleepHours();

} the solution because I am still getting ReferenceError: actualSleepHours is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/sleepDebtCalculator.js:48:6)
at Module._compile (module.js:571:32)

no, its not the solution. Only showed where you declared these variable (within the function), and then you attempt to use the variables outside there intended/existing scope.

Which is why I asked you do this? Why are the comparisons/conditionals not part of the function? They should be. The purpose of the function is to calculate the sleep debt, currently the function only declares two variables. that is it

I’ve been trying to figure out whats wrong for awhile, I did change the function to this

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('You got more sleep than needed');
  } else if(actualSleepHours < idealSleepHours) {
    console.log('You should sleep more');
  } else {
    console.log('Something went wrong!')
  }

};

but I am still getting
if (actualSleepHours < idealSleepHours) {
^

ReferenceError: actualSleepHours is not defined

I put your code here:

JS Bin - Collaborative JavaScript Debugging

seems to work fine, could you post your full code?

const getSleepHours = day => {

switch(day) {

case ‘monday’:

return 8

break;

case ‘tuesday’:

return 8

break;

case ‘wednesday’:

return 8

break;

case ‘thursday’:

return 8

break;

case ‘friday’:

return 8

break;

case ‘saturday’:

return 8

break;

case ‘sunday’:

return 8

break;

default:

return ‘error’;

}

}

console.log(getSleepHours(‘sunday’))

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();

  if (actualSleepHours === idealSleepHours) {

console.log('You got the perfect amount of sleep');

} else if(actualSleepHours > idealSleepHours) {

console.log('You got more sleep than needed');

} else if(actualSleepHours < idealSleepHours) {

console.log('You should sleep more');

} else {

console.log('Something went wrong!')

}

};

if (actualSleepHours < idealSleepHours) {

return console.log(‘You had’ + (idealSleepHours - actualSleepHours) + ’ hours less than you should’ve had.’);

}

your last lines:

if (actualSleepHours < idealSleepHours) {

  return console.log('You had' + (idealSleepHours - actualSleepHours) + ' hours less than you should\'ve had.');

}

you still attempt to use variables outside there existing scope.

1 Like

Okay thanks big mistake on my part, and something ill know for next time.