# 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 ) {
}
``````

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());

``````

``````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,

@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());

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?

``````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?