I completed the Sleep Debt Calculator project and it’s working fine, but I get “undefined” after the expected result.
I reviewed my code and even watched the project walkthrough to see if I did anything different, but still can’t find what is wrong.
Can someone please help?
Here’s my code:
const getSleepHours = (day) => {
switch (day) {
case “monday”:
return 6;
break;
case “tuesday”:
return 7;
break;
case “wednesday”:
return 5;
break;
case “thursday”:
return 5.5;
break;
case “friday”:
return 6.5;
break;
case “saturday”:
return 7;
break;
case “sunday”:
return 7;
break;
default:
return “Error!”;
}
};
if (actualSleepHours === idealSleepHours) {
console.log(“You got the perfect amount of sleep!”);
} else if (actualSleepHours > idealSleepHours) {
console.log(
“You got more sleep than needed. You slept an extra " +
(actualSleepHours - idealSleepHours) +
" hours this week.”
);
} else if (actualSleepHours < idealSleepHours) {
console.log(
“You need more sleep! You slept " +
(idealSleepHours - actualSleepHours) +
" hours less than needed this week.”
);
} else {
console.log(“Error!”);
}
};
The calculateSleepDebt function doesn’t return anything (which is the same as returning undefined),
so when you call the function, you get undefined;
meaning that the result of calculateSleepDebt() is undefined
Therefore, console.log(calculateSleepDebt());
is like doing console.log(undefined);
Since you already have console.log inside the function,
you can just do calculateSleepDebt();
instead of console.log(calculateSleepDebt());
so that you don’t get the undefined on the screen/console.
One could agree with the solution suggested but it goes against the grain of functional programming. We plan and expect a return value from a function call. The above code does not return anything usable, only sediment.
Bottom line, don’t log in the function. Return the value and log the call return at the caller. Even better, assign the return value and then log it, thus retaining the return value for further use. All a part of the functional programming paradigm… Make it useful, simple, and reliable in fulfilling its role.
@mtf mentioned replacing the console.log inside the function with return and then having the console.log outside the function.
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if (actualSleepHours === idealSleepHours) {
return ("You got the perfect amount of sleep!");
} else if (actualSleepHours > idealSleepHours) {
return (
"You got more sleep than needed. You slept an extra " +
(actualSleepHours - idealSleepHours) +
" hours this week."
);
} else if (actualSleepHours < idealSleepHours) {
return (
"You need more sleep! You slept " +
(idealSleepHours - actualSleepHours) +
" hours less than needed this week."
);
} else {
return "Error!";
}
};
// store the result of calling CalculateSleepDebt in a variable so that it can be used later
const sleepDebt = calculateSleepDebt();
console.log(sleepDebt);