Sleep Debt Calculator Help

Any idea why my code won’t return anything? I was expecting It to return 8 and even add up all the days of hours I slept when I called getActualSleepHours.

const getSleepHours = day => {
  switch(day) {
    case 'monday':
      return 8
      break;
    case 'tuesday':
      return 10
      break;
    case 'wednesday':
      return 7
      break;
    case 'thursday':
      return 9
      break;
    case 'friday':
      return 6
      break;
    case 'saturday':
      return 9
      break
    case 'sunday':
      return 7
      break;
    default:
      console.log('error!');

  }
};

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

console.log(getSleepHours('monday'));
console.log(getActualSleepHours());
};

Hello, @css0912954590, and welcome to the forums.

Where did you call getActualSleepHours?

Also, consider arrow function syntax.

//arrow function
const doMoreStuff = () => {
  return 'I did more stuff.'; //need to use 'return' since we have {}'s
}

//concise body arrow function
const doStuff = () => 'I did stuff.' //no {}'s, so no 'return' needed
1 Like

I just updated my profile, sorry it says cssetc, should reflect later today.

I called it in the getActualSleepHours function because the video demo also did it there.

When I call console.log(getSleepHours('monday));
Calling this outside the function, it displays 8.

When I also call console.log(getActualSleepHours()); It displays undefined though.

@midlindner

If there are no function calls outside of the function you are trying to call, it will never be called. Calling a function from within itself is called recursion. If you aren’t very careful, recursion will result in an infinite loop. You don’t want to use recursion here.

Ahhh thank you sir! I figured it out. It’s now displaying 8 and 56 which 56 represents the return of getActualSleepHours.

P.s Anyway to update the username from css09129etc?

1 Like

If you’ve changed your username on the main CC site, you just need to log out of the forums, and log back in. After logging in, your name should be changed.

1 Like

2/2 for you @midlindner

Cheers and thanks again!

1 Like

One last thing that isn’t covered in the exercise. When a return statement is executed, control flow along with the value returned are handed back to the line of code where the function was called (the caller). With that in mind, there is no need for a break statement following a return. The break statements will never be executed. You can remove all of them. Some development environments will give you a warning that you have unreachable code in cases (no pun intended) like this:

2 Likes