''default' isn't working in my switch statement [closed]

the 'default' part of my switch statement in JavaScript isn't logging in my code. I'm getting an 'undefined' if I type in anything else other than a day of the week. I'm new to JavaScript and I'm not sure what's going on.

I've tried to push my knowledge of switch statements by including other alternative entries for 'day'. Everything worked fine until I added these extras and the default value.

function getSleepHours(day) {
  switch (day) {
    case "monday":
    case "Monday":
    case "mon":
    case "Mon":
      return 8;
      break;
    case "tuesday":
    case "Tuesday":
    case "tue":
    case "Tue":
      return 7.5;
      break;
    case "wednesday":
    case "Wednesday":
    case "wed":
    case "Wed":
      return 6.75;
      break;
    case "thursday":
    case "Thursday":
    case "thu":
    case "Thu":
      return 6;
      break;
    case "friday":
    case "Friday":
    case "fri":
    case "Fri":
      return 8.25;
      break;
    case "saturday":
    case "Saturday":
    case "sat":
    case "Sat":
      return 10;
      break;
    case "sunday":
    case "Sunday":
    case "sun":
    case "Sun":
      return 9;
      break;
    default:
      "Re-enter a day of the week only. The following entries are valid: monday, Monday, mon or Mon etc."
  }
};

console.log(getSleepHours("Monday")); //result should = 8
console.log(getSleepHours("td")); //*result should = default message, but logs 'undefined'.*
console.log(getSleepHours("Wednesday")); //result should = 6.75
console.log(getSleepHours("thursday")); //result should = 6
console.log(getSleepHours("fri")); //result should = 8.25
console.log(getSleepHours("saturday")); //result should = 10
console.log(getSleepHours("Sun")); //result should = 9


Solution 1:[1]

You were missing the return in the default

All that code could be written like this

const vals = { "sun": 9, "mon": 8, "tue": 7.5, "wed": 6.75, "thu": 6, "fri": 8.25, "sat": 10 };

const getSleepHours = day => {
  day = day.toLowerCase().slice(0, 3); 
  const sleep = vals[day]; // if found 
  return sleep || "Re-enter a day of the week only. The following entries are valid: monday, Monday, mon or Mon etc.";
};

console.log(getSleepHours("Monday")); //result should = 8
console.log(getSleepHours("td")); //*result should = default message, but logs 'undefined'.*
console.log(getSleepHours("Wednesday")); //result should = 6.75
console.log(getSleepHours("thursday")); //result should = 6
console.log(getSleepHours("fri")); //result should = 8.25
console.log(getSleepHours("saturday")); //result should = 10
console.log(getSleepHours("Sun")); //result should = 9

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mplungjan