This is not working it says the the actualSleepHours is undefined even thougrh is clreay defined

const getSleepHours = day => {                 
switch (day)   {                                case 'monday':  
  return 8
  break;
  case 'tuesday':
    return 5
    break;
  case 'wensday':
    return 2
    break;
  case 'thursday':
    return 6
    break;
  case 'friday':
    return 8
    break;
    case 'satuday':
    return 6
    break;
  case 'sunday':
    return 3
    break; } 
};
const getActualSleepHours = () =>                  getSleepHours('monday')+                          getSleepHours('tuesday')+                        getSleepHours ('wensday')+                        getSleepHours ('thursday')+                      getSleepHours ('friday')+                        getSleepHours ('satuday')+                        getSleepHours ('sunday');
const getIdealSleepHours = () => {
  let idealHours = 8.3;
  return idealHours * 7;
};
const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours(); 
  const idealSleepHours = getIdealSleepHours();
};
if (actualSleepHours === idealSleepHours) {    
  console.log ('You have goten the perfect amount of sleep now wake the up you lazy scumbag')};

odecademy.com/paths/create-video-games-with-phaser/tracks/game-dev-learn-javascript-function-and-scope/modules/game-dev-learn-javascript-functions/projects/sleep-debt-calculator

Hey @blog1287923261, welcome to the forum.

I formatted your code. Go ahead and check out this post as a reference on how to do this for future topics.

Your issue is one of scope.

Your actualSleepHours variable does not exist outside of your calculateSleepDebt function, but your if statement - if (actualSleepHours === idealSleepHours) - is not enclosed in the function. As a result, it’s attempting to use a variable which does not exist in the global scope.

The JavaScript course has an entire lesson on scope. :slight_smile:

thank you so much i have fixed the problem thanks to you

1 Like

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