Calling sleepDebtCalculator, but nothing happens


#1

I think I’ve completed the exercise, however when I run the code, nothing happens. No errors, nothing at all.

Can someone please check me here? Thank you!

https://www.codecademy.com/courses/learn-javascript-functions/projects/sleep-debt-calculator?course_redirect=introduction-to-javascript

const getSleepHours = (day) => {
switch (day) {
  case 'monday':
  return 8;
  case 'tuesday':
  return 9;
  case 'wednesday':
  return 6;
  case 'thursday':
  return 5;
  case 'friday':
  return 9;
  case 'saturday':
  return 8;
  case 'sunday':
  return 10;
  default: 
  console.log("Sorry, what day was that again?");
};
           }
    
const getActualSleepHours = () => { return
getSleepHours('monday') +
getSleepHours('tuesday') + 
getSleepHours('wednesday') + 
getSleepHours('thursday') + 
getSleepHours('friday') + 
getSleepHours('saturday') + 
getSleepHours('sunday');
                                  }

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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours ();
if (actualSleepHours === idealSleepHours) {console.log('You got just the right amount of sleep!');
  } else if  (actualSleepHours > idealSleepHours) {console.log('You slept a little too much this week! To be exact you overslept by ' +  (actualSleepHours - idealSleepHours) + ' hours.');
  } 
   else if (actualSleepHours < idealSleepHours) {console.log('You need to get more sleep mayne! To be specific, you need to get ' +  (idealSleepHours - actualSleepHours) +' more hours of sleep.');
                                          }          
            };

calculateSleepDebt ();

#2

Hi @papercrane01,

Here, because this part of your code:

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

Basically, what happens is that JavaScript automatically insert a semicolon right after your return (because there’s no following code in the same line) as the getSleepHours(...) function is written in a newline, the function exits too early after the return but before getSleepHours(....), causing your code for the rest of getSleepHours(…) not executed. You can read more with the links I provided below.

It is said white spaces doesn’t matter like how you’d write:

getSleepHours('monday') +            getSleepHours('tuesday') +        
 getSleepHours('wednesday') +  ........ 

It is the same with:

getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + .......

However, in your case, this is special, it is related to how JS works called JavaScript’s automatic semicolon insertion (ASI)

So change your code into writing the return code in the same line (for safer purpose) or you can wrap it with additional brackets/parenthesis (which is more confusing to me):

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

The rest of the code would work just fine.


For more reading about questions for JavaScript’s automatic semicolon insertion (ASI)



Cheers :slight_smile:


#3

Wow! Yes, that fixed it. Great catch! Thanks for making me aware of ASI, that likely wouldn’t have been the last time :wink: