Sleep Debt Calculator - help with Switch statement

What is wrong with my Switch statement? My code works fine when I use the if/else statement, but my switch statement just returns “undefined” for all values. What am I doing wrong?

// const getSleepHours = day => {
// if (day === ‘monday’) {
// return 8;
// } else if (day === ‘tuesday’) {
// return 7;
// } else if (day === ‘wednesday’) {
// return 6;
// } else if (day === ‘thursday’) {
// return 5;
// } else if (day === ‘friday’) {
// return 8;
// } else if (day === ‘saturday’) {
// return 7;
// } else {
// return 8;
// }
// }

const getSleepHours = day => {
switch (day) {
case ‘monday’: 8;
break;
case ‘tuesday’: 7;
break;
case ‘wednesday’: 6;
break;
case ‘thursday’: 5;
break;
case ‘friday’: 8;
break;
case ‘saturday’: 7;
break;
case ‘sunday’: 8;
break;
}
}

console.log(getSleepHours(‘monday’));
console.log(getSleepHours(‘tuesday’));
console.log(getSleepHours(‘wednesday’));
console.log(getSleepHours(‘sunday’));

I don’t see any return keywords when you are using the switch statement?

You’re right, the return keyword works. For some reason, I had thought it was implied in the switch statement syntax. Learn something every day!

Question - do I need the break keyword if I use “return”? Or is that redundant?

Yes that would be redundant! The break would not be reached at all.

1 Like