sleepDebt Calculator swtich statement

Hello,
I am working on Sleep Debt Calculator and am trying to use switch statement where the hints use if/else. The switch statement I am referring to is the bottom one. I know python fairly well but am brand new to JS syntax. Can I even input 2 variables into switch statement like this? I like the switch statements over if/else but would it just be far easier to use if/else?
I don’t get an error so im assuming this is appropriate syntax but I just get undefined when i try printing the calcSleepDebt function call
https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-functions/projects/sleep-debt-calculator

getSleepHours = day => {
  switch (day) {
  case 'monday':
    return 6;
  case 'tuesday':
    return 6;
  case 'wednesday':
    return 6;
  case 'thursday':
    return 6;
 case 'friday':
    return 6;
  case 'saturday':
    return 6;
  case 'sunday':
    return 6;
  }
}
getActualSleepHours = () =>
  getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');

getIdealSleepHours = () => {
  const idealHours = 8
  return idealHours * 7;
}
console.log(getActualSleepHours())
console.log(getIdealSleepHours())
calcSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  switch (actualSleepHours, idealSleepHours) {
    case (actualSleepHours === idealSleepHours):
      console.log('You got perfect sleep');
      break;
    case (actualSleepHours > idealSleepHours):
      console.log('You got ' + (actualSleepHours - idealSleepHours) + ' hours more sleep than you should.');
      break;
    case (actualSleepHours < idealSleepHours):
      console.log('You got ' + (idealSleepHours - actualSleepHours));
      break;  
  }
}

console.log(calcSleepDebt())

Thank you !

Unfortunately switch statements can only take one expression as input hence why there’s nothing being logged to the console from the it. I also like switch however it’s not appropriate in every situation, generally I find it to be better when working with an integer value that can take one of many values, or just generally I like it for when there’s more than 4 cases that need to be considered. I would recommend just using an if/else statement instead.

As for why it’s giving ‘undefined’, this would happen even with if/else statements as you are logging the calcSleepDebt() call to the console. As this function doesn’t actually return anything, it’s going to print undefined as this is the value of the function. You either just call the function, or you change the console.logs inside of the function to return's.
I think it’s totally fine to leave the console logs in this situation however using return's would mean that the function would exist as soon as the correct condition has been met, and as such it would effectively work the same way the switch statement does currently.

2 Likes

Hi @core2761115992
considering what @adamgaffney96 wrote about just being able to pass one value to a switch statement and returning from the function in order to avoid getting ‘undefined’ due to a double log, you could still stick to the switch case solution rather than if else statement by adding just one const to the function, which returns this single value you need. It still stays quite short this way compared to if else statements:

calcSleepDebt = () => {
	const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  const sleepHoursGap = Math.sign(actualSleepHours - idealSleepHours);
  switch (sleepHoursGap) {
    case 0:
      return 'You got perfect sleep';
    case 1:
       return 'You got ' + (actualSleepHours - idealSleepHours) + ' hours more sleep than you should.';
    case -1:
       return 'You got ' + (idealSleepHours - actualSleepHours) + ' hours less sleep than you should.';
  }
}
1 Like