Help with Sleep Debt Calculator

Please can someone help me find the cause of this error message that this code causes;

const getSleepHours = (day) => {

  

  switch (day) {

  case "Monday":

  return 6;

  break;

  case "Tuesday":

  return 6;

  break;

  case "Wednesday":

  return 7;

  break;

  case "Thursday":

  return 7;

  break;

  case "Friday":

  return 8;

  break;

  case "Saturday":

  return 7;

  break;

  case "Sunday":

  return 9;

  break;

default:

return "Error."

 

  }

};

//console.log(getSleepHours("Sunday"));

const getActualSleepHours = () => {

getSleepHours("Monday")

+ getSleepHours("Tuesday")

  + getSleepHours("Wednesday")

    + getSleepHours("Thursday")

      + getSleepHours("Friday")

        + getSleepHours("Saturday")

          + getSleepHours("Sunday") 

}

//because this function doesn't have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there's no parameter value, it will not be required to get the result in the log. 

const getIdealSleepHours = () = {//NewFunctionNme

  let idealHours = 7// Created New Variable.

  return idealHours * 7;// Aplied Maths to it.

};

const calculateSleepDebt = () => {//L" NewV

  let actualSleepHours = getActualSleepHours();

  const idealSleepHours = getIdealSleepHour();

  // const idealSleepHours also nV

if(actulaSleepHours === idealSleepHours){ console.log( "You done Good.");

}

else if(actualSleepHours >  idealSleepHours){console.log("You done Bad!")}

}

elseif(actualSleepHours < idealSleepHours){

  console.log("kick back and chill. You work to Hard.")

}

else { console.log("Error. Something went wrong. Check Your Code.") // This else statment will get evaluated if none of the previous conditions are true.

}

};

calculateSleepDebt();

How are you still on this project?

anyway, your function declaration is off/wrong, slightly different this time:

const getIdealSleepHours = () = {//NewFunctionNme

does that look right to you? Compare it to your other function declarations. You should be able to lean from the error messages, even though the error might not be 100% identical, its again related to function declaration

const getSleepHours = (day) => {////// I'm on the next one now. I can't see if for the life of me?

  

  switch (day) {

  case "Monday":

  return 6;

  break;

  case "Tuesday":

  return 6;

  break;

  case "Wednesday":

  return 7;

  break;

  case "Thursday":

  return 7;

  break;

  case "Friday":

  return 8;

  break;

  case "Saturday":

  return 7;

  break;

  case "Sunday":

  return 9;

  break;

default:

return "Error."

 

  }

};

//console.log(getSleepHours("Sunday"));

const getActualSleepHours = () => {

getSleepHours("Monday")

+ getSleepHours("Tuesday")

  + getSleepHours("Wednesday")

    + getSleepHours("Thursday")

      + getSleepHours("Friday")

        + getSleepHours("Saturday")

          + getSleepHours("Sunday") 

}

//because this function doesn't have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there's no parameter value, it will not be required to get the result in the log. 

const getIdealSleepHours = () => {//NewFunctionNme

  let idealHours = 7// Created New Variable.

  return idealHours * 7;// Aplied Maths to it.

};

const calculateSleepDebt = () => {//L" NewV

  let actualSleepHours = getActualSleepHours();

  const idealSleepHours = getIdealSleepHour();

  // const idealSleepHours also nV

if(actulaSleepHours === idealSleepHours){ console.log( "You done Good.");

}

else if(actualSleepHours >  idealSleepHours){console.log("You done Bad!")}

}

else if(actualSleepHours < idealSleepHours){console.log("Kick back and chill You work too Hard")

}

else { console.log("Error. Something went wrong. Check Your Code.") // This else statment will get evaluated if none of the previous conditions are true.

}

};

console.log(calculateSleepDebt());

well, here:

else if(actualSleepHours >  idealSleepHours){console.log("You done Bad!")}

}

you close both the else if and your function. (two }, do you see?)

you should be more consistent with the placing and indention of your curly brackets to prevent exactly this kind of error. A style many programmers follow in JavaScript is:

if ( condition ) {
   // body of your clause
}

I would recommend you to do the same.

Yeah thanks for getting back to me. I was struggling to remember what the empty else if statement looks like. I will go back and find those lesson notes.
Tom

Still Not Going Through?

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

default:
return "Error."
 
  }
};

//console.log(getSleepHours("Sunday"));

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

//because this function doesn't have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there's no parameter value, it will not be required to get the result in the log. 

const getIdealSleepHours = () => {//NewFunctionNme
  let idealHours = 7// Created New Variable.
  return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
  let actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHour();
  // const idealSleepHours also nV

if(actulaSleepHours === idealSleepHours){ console.log( "You done Good.")
} else if (actualSleepHours >  idealSleepHours) { (console.log("You done Bad!")
}else if(actualSleepHours < idealSleepHours){ console.log("Kick back and chill You work too Hard")
} else {
  console.log("Error. Something went wrong. Check Your Code.")
  }; // This else statment will get evaluated if none of the previous conditions are true.


console.log(calculateSleepDebt());


your still inconsistent in your indention. Here:

else {
  console.log("Error. Something went wrong. Check Your Code.")
  };

the body of your clause on its own line, while the rest is not.

The indention is off/wrong anyway. Given these conditions are part of the body of the function, all the conditions should have indention to.

having this concise indention, you might have noticed you forgot to close your function (missing })

So I put the Console.log on the same, Then I put the cursor behind Each Else Statment and Pressed Tab, Still got the error message…

Can I see what you did? I have no idea what changes you made.

I feel flattered that you regard me this highly, that now I can magically guess how its still possible you have an error.

I deleted the last two else if statements and it still came back with an error?

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

default:
return "Error."
 
  }
};

//console.log(getSleepHours("Sunday"));

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

//because this function doesn't have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there's no parameter value, it will not be required to get the result in the log. 

const getIdealSleepHours = () => {//NewFunctionNme
  let idealHours = 7// Created New Variable.
  return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
  let actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHour();
  // const idealSleepHours also nV

  if(actualSleepHours === idealSleepHours){
    console.log("You Done Good.")
  }
  }; //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.


console.log(calculateSleepDebt());

I know error messages might look terrifying, but they usually contain useful information.

The error tells me there is something with this function call:

const idealSleepHours = getIdealSleepHour();

If I then look at the method definition of the method you attempt to call:

const getIdealSleepHours = () => {//NewFunctionNme

I see can a grammar/typo mistake.

You really need to learn to debug, debugging is part of programming.

Might be caused by our digital communication and not knowing each other, but now it comes across as: I have an error, and I don’t know, please fix it for me.

Being a developer is not only about code, its also about coming up with theories/hypothesis what might cause this error, and how you go about solving the error

1 Like

I definitely see the difference in the two calls. There was a reference to the types of text used in a function… i couldn’t remember what lesson it was, so I just. tried my best to make a camel cast version, of the text… but i do need clarity on the types of text and when to apply them. ThankYou

Two calls? I talked about a function definition/declaration and a function call.

What do you mean by types of text?

What I meant was, “The Case in the Lettering” counts as one call… and then I thought you said something about having to indent those If statements… but it O.K. now because, i altered the lettering. This is the new error message UNDEFINED I can’t work it out. Can you help please?

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

default:
return "Error."
 
  }
};

//console.log(getSleepHours("Sunday"));

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

//because this function doesn't have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there's no parameter value, it will not be required to get the result in the log. 

const getIdealSleepHours = () => {//NewFunctionNme
  let idealHours = 7// Created New Variable.
  return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
  let actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  // const idealSleepHours also nV

  if(actualSleepHours === idealSleepHours){
    console.log("You Done Good.")
  }
   //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.

};
console.log(calculateSleepDebt());

Hi @ithomas123,

Please paste your code like this so it’s a lot easier to read in the forums:

@ithomas123 your post was modified with the proper markup to make it easier to read. Going forward, it would be good to learn how to post code in the forum like in the animation I posted above.

As for what you wrote about, the good news is that undefined is not an error message.

You saw undefined in the console because of this line: console.log(calculateSleepDebt());

That means that your calculateSleepDebt() function returned undefined automatically because you didn’t return a value or you returned undefined on purpose.

If we take a look at your calculateSleepDebt() function:

const calculateSleepDebt = () => {//L" NewV
  let actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  // const idealSleepHours also nV

  if(actualSleepHours === idealSleepHours){
    console.log("You Done Good.")
  }
   //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.

};

there is no return statement. That’s why undefined was logged to the console. What did you want to return here?

Hi thanks for you message. I thought maybe, the IF statement is returning the UNDEFINED message because of the disagreement in the Values as well, so I altered them in both the getSleep function and idealHours, so that the values were equal and, then would meet then condition of the IF Statement…
So surely the condition has been met! So why won’t it print the string?
When I pressed the Button in the Video it said Blockquote? and I don’t know what that does I dont really understand why you want me to press the button. But it could Alter the CODE!

Blockquote

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;

default:
return “Error.”

}
};

//console.log(getSleepHours(“Sunday”));

const getActualSleepHours = () => {
getSleepHours(“Monday”)

  • getSleepHours(“Tuesday”)
    • getSleepHours(“Wednesday”)
      • getSleepHours(“Thursday”)
        • getSleepHours(“Friday”)
          • getSleepHours(“Saturday”)
            • getSleepHours(“Sunday”)
              }

//because this function doesn’t have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there’s no parameter value, it will not be required to get the result in the log.

const getIdealSleepHours = () => {//NewFunctionNme
let idealHours = 7// Created New Variable.
return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
let actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
// const idealSleepHours also nV

if(actualSleepHours === idealSleepHours){
console.log(“You Done Good.”)
}

//, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.
};

console.log(calculateSleepDebt());

image

This is the button I wanted you to press. It then puts the characters you need to make it so the code can be read.

You press that button, replace the “type or paste code here” text with your code, and you leave the other characters it added.

So surely the condition has been met! So why won’t it print the string?
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;

default:
return "Error."
 
  }
};

//console.log(getSleepHours("Sunday"));

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

//because this function doesn't have a parameter, (see these brackets are full in the first function but empty in the second) we do not need to pass in an argument. I.e what this means is, getActualSleepHours added up the totals from each day. and Remember, if there's no parameter value, it will not be required to get the result in the log. 

const getIdealSleepHours = () => {//NewFunctionNme
  let idealHours = 7// Created New Variable.
  return idealHours * 7;// Aplied Maths to it.
};

const calculateSleepDebt = () => {//L" NewV
  let actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  // const idealSleepHours also nV

  if(actualSleepHours === idealSleepHours){
    console.log("You Done Good.")
  }
  
   //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.
};

console.log(calculateSleepDebt());type or paste code here
1 Like

There you go, you got code posting down.

Here are the couple issues you need to solve.

  1. You’re still going to have the issue of undefined being printed to the console because your calculateSleepDebt() isn’t returning a value. I posted about this previously and you can read that.

  2. Since you’re also concerned that your message of “You Done Good.” didn’t get printed to the console, we need to look at why that is.

The best way to do that is to temporarily add more logging to the function so we know what the values are that we’re comparing.

For example, we could add these to our calculateSleepDebt() function:

console.log('actual hours ' + actualSleepHours);
console.log('ideal hours ' + idealSleepHours);

They should be equal right?

You could add them to your function here:

const calculateSleepDebt = () => {//L" NewV
  let actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  // const idealSleepHours also nV

  console.log('actual hours ' + actualSleepHours);
  console.log('ideal hours ' + idealSleepHours);

  if(actualSleepHours === idealSleepHours){
    console.log("You Done Good.")
  }
  
   //, This is the Closing Bracket Related to the Original Function. This else statment will get evaluated if none of the previous conditions are true.
};

Were the values surprising? Did you get any clues about what one of your other functions might be missing?