Sleep debt calculator - nothing is output by my last function in this code

Can anybody see why no if else statment come from the last function in my code. It doesn’t kick back any errors and i’m very confused.

const getSleepHours = (day) => {
  switch (day) {
    case "Monday":
      return 8;

    case "Tuesday":
      return 5;

    case "Wednesday":
      return 7;

    case "Thursday":
      return 4;

    case "Friday":
      return 8;

    case "Saturday":
      return 8;

    case "Sunday":
      return 9;

    default:
      return "Error!!";
  }
};

const getActualSleepHours = () =>
  getSleepHours("Monday") +
  getSleepHours("Tuesday") +
  getSleepHours("Wednesday") +
  getSleepHours("Thursday") +
  getSleepHours("Friday") +
  getSleepHours("Saturday") +
  getSleepHours("Sunday");

const getIdealSleepHours = () => {
  let idealHours = 6;
  return idealHours * 7;
};
console.log("Ideal sleep " + getIdealSleepHours());
console.log("Actual sleep " + getActualSleepHours());

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();

  if (actualSleepHours === idealSleepHours) {
    console.log("You got the perfect amount of sleep.");
  } else if (actualSleepHours > idealSleepHours) {
    console.log("You slept too much sleepy head!");
  } else if (actualSleepHours < idealSleepHours) {
    console.log("You need to sleep more.");
  } else {
    console.log("An error happened");
  }
};

Hi there,

Are you calling calculateSleepDebt(); ?

oh no, man I spent so long error and spelling checking this. Of course!!

i’m not calling it, thanks.

1 Like

Hi, I was having trouble with the Sleep Debt Calculator returning “undefined”. The solution here works, but it is quite a bit different from the
video walk through code that included “*break;” after “case” and “return”.
code from walk through:
const getSleepHours = day => {
switch(day) {
case ‘monday’:
return 8
break;
case ‘tuesday’:
return 7
break;
How did you get to your format of:

case "Thursday":
      return 4;

    case "Friday":
      return 8;

Hi there,

I would not know why your code returned undefined without fully seeing it. But to answer your other question. When a condition is met in a case switch, it will execute all code within the block until it meets an exit statement. Both break and return are exit statements.

Consider this function:

function foo(word) {
  switch(word) {
    case 'word1':
      return "you said word1!";
    case 'word2':
      console.log("If you said word2 without a break then this will continue");
    case 'word3':
      console.log("to log the code for word3");
  }
}

Because word1 is using an exit statement, it will not run the code for the following two case switches.

image

But because there is no exit command following word2, both lines of code will run.

image

Now if that break; is included, only one line will run.

function foo(word) {
  switch(word) {
    case 'word1':
      return "you said word1!";
    case 'word2':
      console.log("If you said word2 without a break then this will continue");
      break;
    case 'word3':
      console.log("to log the code for word3");
  }
}

image

2 Likes

Thank you! That makes sense now.

1 Like