Hi Can’t get the following Script to Stop Producing the word Undefined. Although it is producing the correct string. Can anyone help me with 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;[day]
default:
return "Error."
}
};
//console.log(getSleepHours("Sunday"));
const getActualSleepHours = () => {
return getSleepHours("Monday")
+ getSleepHours("Tuesday")
+ getSleepHours("Wednesday")
+ getSleepHours("Thursday")
+ getSleepHours("Friday")
+ getSleepHours("Saturday")
+ getSleepHours("Sunday")
};
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());
mtf
November 24, 2020, 11:27pm
#2
If you are logging within a function and then log the return from that function it will be undefined
. return
from the function and log at the caller and that will go away.
1 Like
I think that I understand some of what your saying, But
return
from the function and log at the caller and that will go away.
I don’t understand what do you mean “return” from the function?
Also, what is the caller?
mtf
November 25, 2020, 2:57am
#4
The function is the callee and the expression that calls it is the caller .
if(actualSleepHours === idealSleepHours){
console.log("You Done Good.") // < change to return
}
};
console.log(calculateSleepDebt()); // < caller logs return value
The thing to look at is the calculateSleepDebt()
function. It has no return value, therefore JS returns, undefinded
by default.
If in the above we write,
return "You done good!"
and before the closing brace, write,
return "A little off, there."
or some such, then your function will have explicit return values for both cases.
You Can’t Change that to another Return, because its part of a bigger If statement.
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;[day]
default:
return "Error."
}
};
//console.log(getSleepHours("Sunday"));
const getActualSleepHours = () => {
return getSleepHours("Monday")
+ getSleepHours("Tuesday")
+ getSleepHours("Wednesday")
+ getSleepHours("Thursday")
+ getSleepHours("Friday")
+ getSleepHours("Saturday")
+ getSleepHours("Sunday")
};
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.")
}else if (actualsleephours > idealsleephours) {
console.log("user got more sleep then needed")
}else if (actualsleephours < idealsleephours){
console.log ("you should get some rest")
}else{ console.log("Error. Something went wrong. Check you code."){
};
console.log(calculateSleepDebt());
mtf
November 25, 2020, 3:09am
#6
Then change them all, and drop the else
in all cases.
if … return …
if … return …
if … return …
return “Error. …”
The idea is to get away from logging inside the function.
It’s either that, or simply don’t log the call expression at the bottom. Just invoke it.
foo()
i changed them, but still getting an error…?
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;[day]
default:
return "Error."
}
};
//console.log(getSleepHours("Sunday"));
const getActualSleepHours = () => {
return getSleepHours("Monday")
+ getSleepHours("Tuesday")
+ getSleepHours("Wednesday")
+ getSleepHours("Thursday")
+ getSleepHours("Friday")
+ getSleepHours("Saturday")
+ getSleepHours("Sunday")
};
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){
return("You Done Good.")
}else if (actualsleephours > idealsleephours) {
return("user got more sleep then needed")
}else if (actualsleephours < idealsleephours){
return ("you should get some rest")
}else{ return
("Error. Something went wrong. Check you code."){
}
}
};
console.log(calculateSleepDebt());
mtf
November 25, 2020, 3:16am
#8
What is the error message? Is in the console or is it an SCT message?
mtf
November 25, 2020, 3:19am
#10
There is some stray syntax (the curly braces) and return
cannot be on its own line followed by a line break.
return expression
must be on the same line.
Aside
return
values don’t have to be in parens. It is not a function.
The first condition works. That Undefined has gone. But when I changed the values in the actualsleephours Function it returned another error message?
mtf
November 25, 2020, 3:33am
#12
ithomas123:
actualSleepHours
Compare that to the error message. An easy fix.
1 Like
system
closed
January 5, 2021, 7:33pm
#13
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.