Why isn't this variable logging to the console?

Please see my code below. Why isn’t the variable “actualSleepHours” logging to the console? (it’s the last line of code)

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

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


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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  
  if (actualSleepHours === idealSleepHours) {
    console.log('You got just enough');
  } else if (actualSleepHours > idealSleepHours) {
      console.log('You slept too much');
    } else if (actualSleepHours < idealSleepHours) {
      console.log('You need more sleep');
    } else {
      console.log('Error');
    }
  }

calculateSleepDebt();
  console.log('You slept ' + actualSleepHours);

actualSleepHours doesn’t exist in the global scope, you have defined it inside calculateSleepDept, so you can access it only inside that function

1 Like