Project: Sleep Debt Calculator (Stuck at Step 5)

Working on JavaScript Project “Sleep Debt Calculator” and stuck at Step 5. When reviewing the forums of others’ input, I noticed that my structure is slightly different from the majority (I’m not using the ternary function format).

Why does my code fail to work similarly?

My console produces “[Function: getActualSleepHours]” rather than adding up the hours of each day.
See my code below:

function getSleepHours(day) {
  switch (day) {
    case 'monday':      return 8;
    case 'tuesday':     return 7;
    case 'wednesday':   return 8;
    case 'thursday':    return 7;
    case 'friday':      return 7;
    case 'saturday':    return 9;
    case 'sunday':      return 9; 
  }
};
function getActualSleepHours() {
  getSleepHours('monday') 
  + getSleepHours('tuesday')
  + getSleepHours('wednesday')
  + getSleepHours('thursday')
  + getSleepHours('friday')
  + getSleepHours('saturday')
  + getSleepHours('sunday');
  return getActualSleepHours;
};
console.log(getActualSleepHours());

You’re not returning the sum of all the function calls you made to get the total sleep hours. Instead, you’re returning the getActualSleepHours function, so that’s what is printing to the console.

1 Like

Thanks for your reply!

That makes sense although, I’m still unsure sure what the correct solution is here. If I remove the return statement, my console produces “undefined”.

What am I missing here?

Your goal is to return the result of you adding all those values together. You’re already adding them, but you don’t do anything with the value. You could assign the value to a variable and then return the variable, or you could return the sum directly.

Click for an extra hint

Even though they are on different lines, JavaScript will treat them the same as if they were on one line since you are adding and because you only use the semi-colon at the end (which is good).

So you could add return before the first call or you could store the value to a variable and then return that variable
example

function testing() {
  return getSomething()
   + getSomethingElse();
}

//or 

function testing() {
  let value = getSomething()
   + getSomethingElse();
  return value;
}

1 Like

I got it! Thank you for your help and patience!

My solution:
function getActualSleepHours() {
  let getActualSleepHours =
  getSleepHours('monday') 
  + getSleepHours('tuesday')
  + getSleepHours('wednesday')
  + getSleepHours('thursday')
  + getSleepHours('friday')
  + getSleepHours('saturday')
  + getSleepHours('sunday');
  return getActualSleepHours;
};
console.log(getActualSleepHours());
1 Like

You’re welcome!

Also, while that will work, I would suggest using a different variable name than the same name as your function in the future. It could lead to confusion as your code gets more complex.

1 Like

Great suggestion. Will do!