Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

185
Views
'default' isn't working in my switch statement

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!