Sleep debt help

Hi,can someone help me spot what I’m doing wrong. I’ve checked with the walkthrough and it looks the same to me.

 const getSleepHours = day => {
  switch (day) {
    case 'Monday':
      return 8
      break;
    case 'Tuesday':
      return 7
      break;
    case 'Wednesday':
      return 5
      break;
    case 'Thursday':
      return 6
      break;
    case 'Friday':
      return 8
      break;
    case 'Saturday':
      return 9
      break;
     case 'Sunday':
      return 7
      break;
    default:
      return 'Error'
  }
};

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 = () => {
  const idealHours = 8;
  return idealHours * 7;
};

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

if (actualSleepHours === idealSleepHours) {
  console.log('You got the ideal amount of sleep');
} else 
if (actualSleepHours > idealSleepHours) {
  console.log('You got too much sleep');
} else {
  console.log('You didnt get enough sleep');
};

calculateActualSleepDebt();

I get an error that actualSleepHours is undefined.

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

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

you defined actualSleepHours here:

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

so you have a function with block scoped variable (which only exists in the function)

then after the function, you attempt to use this variables in conditionals.

1 Like

Thanks, that fixed it.