Why does it return undefined

Why does it say undefined when it logs in the console?

const getSleepHours = day => {
  switch(getSleepHours){
    case 'Monday': return 7;
    break;
    case 'Tuesday': return 7;
    break;
    case'Wednesday': return 7;
    break;
    case 'Thursday': return 7;
    break;
    case 'Friday': return 7;
    break;
    case 'Saturday': return 7;
    break;
    case 'Sunday': return 7;
    break;
  }
}
getSleepHours();
console.log(getSleepHours('Tuesday'))

In the first line, you’re defining a fat arrow function named getSleepHours, that takes one parameter. Within the function, you name that parameter day.

So what do you think your switch statement should be checking? I mean, what should go in the switch parentheses?

2 Likes

Hello Ethan, which result did you expect / want to get on the console?

You have asked for getSleepHours in your switch statement, and getSleepHours is a variable that points towards the function you are creating, and you are actually referring to it directly so you would need a case that points towards the same function , and this is certainly not what you want, but I hope this explains to you as to what you are currently doing.

Your code should have parentesis in “day” because it’s a function parameter and the switch should take “day” as a parameter, not the function. So, it should be like this:

const getSleepHours = (day) => {
  switch(day){
    case 'Monday': return 7;
    break;
    case 'Tuesday': return 7;
    break;
    case'Wednesday': return 7;
    break;
    case 'Thursday': return 7;
    break;
    case 'Friday': return 7;
    break;
    case 'Saturday': return 7;
    break;
    case 'Sunday': return 7;
    break;
  }
}
getSleepHours();
console.log(getSleepHours('Tuesday'))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.