Help with Sleep Debt Calculator Java Script

Please can someone show me how to do Step 5. " At Present getting the Following Error Message. Code I’ve written so far below snippet:

Also the Reason I left the Curly Bracket of the last Function, is She said in the Video you should leave the Curly Bracket Off for an Implicit Return:

const getSleepHours = (day) => {
  
  switch (day) {
  case "Monday":
  return 6;
  break;
  case "Tuesday":
  return 6;
  break;
  case "Wednesday":
  return 6;
  break;
  case "Thursday":
  return 6;
  break;
  case "Friday":
  return 6;
  break;
  case "Saturday":
  return 7;
  break;
  case "Sunday":
  return 8;
  break;

default:
return "Error."
 
  }
};


// console.log(getSleepHours("Saturday"));
const getActualSleepHours = () => 
getSleepHours("Monday")
+ getSleepHours("Tuesday")
  + getSleepHours("Wednesday")
    + getSleepHours("Thursday")
      + getSleepHours("Friday")
        + getSleepHours("Saturday")
          + getSleepHours("Sunday")  
      
  
console.log(getSleepHours("tuesday"));

I think there’s actually nothing wrong with your getActualSleepHours() function.
The reason why you’re getting Error is because of this call to getSleepHours():

In your switch statement you said: if the value of day is ‘Tuesday’ return 6. However, you’re passing ‘tuesday’ (with a lowercase ‘t’) as an argument instead of ‘Tuesday’, therefore it prints what you defined in the default statement: Error.

Hope this helps! :slightly_smiling_face:

1 Like

@yizuhi is right. I always find it helpful to do something like this:

return `Error for day: ${day}`

so you know for which day you get an error. Now you only know you get an error, but you don’t know how and why.

Also, I disagree with the video. I would use curly brackets and explicit return, the body of this function is too big for a implicit return IMHO.

2 Likes

Thanks for the Help Yesterday, I’m still a bit stuck. This is the Question, the second part:

5/ inside the getActualSleepHours() function, call the getSleepHours() function for each day of the week. Add the results together and return the sum using an implicit return .

When I tried to add the two function, and print them in a Console.Log it gave me a wrong answer, and error message. The correct answer should be about 51,

const getSleepHours = (day) => {
  
  switch (day) {
  case "Monday":
  return 6;
  break;
  case "Tuesday":
  return 6;
  break;
  case "Wednesday":
  return 6;
  break;
  case "Thursday":
  return 6;
  break;
  case "Friday":
  return 6;
  break;
  case "Saturday":
  return 7;
  break;
  case "Sunday":
  return 8;
  break;

default:
return "Error."
 
  }
};


// console.log(getSleepHours("Saturday"));
const getActualSleepHours = () => 
getSleepHours("Monday")
+ getSleepHours("Tuesday")
  + getSleepHours("Wednesday")
    + getSleepHours("Thursday")
      + getSleepHours("Friday")
        + getSleepHours("Saturday")
          + getSleepHours("Sunday") 
         
         console.log(getActualSleepHours() + getSleepHours());

I think you skipped step 6:

To get the ideal sleep hours that you prefer, create a function named getIdealSleepHours with no parameters.

Inside the function, declare a variable named idealHours and set its value to your ideal hours per night. Then return the idealHours multiplied by 7.

You’ll want to multiply by 7 to get the total hours you prefer per week.

altogether. I don’t see a getIdealSleepHours function?

I was just wandering about step 5. I haven’t got to step 6 Yet,

5/ inside the getActualSleepHours() function, call the getSleepHours() function for each day of the week. Add the results together and return the sum using an implicit return .

the part where it says, Add the results together? How would you do that?

you have already done this:

const getActualSleepHours = () => 
getSleepHours("Monday")
+ getSleepHours("Tuesday")
  + getSleepHours("Wednesday")
    + getSleepHours("Thursday")
      + getSleepHours("Friday")
        + getSleepHours("Saturday")
          + getSleepHours("Sunday") 

you add (+) the result of all the calls together.

1 Like

And then you can just call getActualSleepHours:

console.log(getActualSleepHours())
1 Like