Sleep Debt Calculator runs the way it should - except for the undefined output


#1

Here’s the code: It does what it is supposed to, but can someone please tell me why I’m getting the “undefined” logged to the console? Many thanks!

const getSleepHours = (day) => {
if(day===‘Monday’){
return 8;
}else if(day===‘Tuesday’){
return 8;
}else if(day===‘Wednesday’){
return 8;
}else if(day===‘Thursday’){
return 8;
}else if(day===‘Friday’){
return 8;
}else if(day===‘Saturday’){
return 8;
}else{ //Sunday
return 8;
}
};
//console.log(getSleepHours(‘Wednesday’));
const getActualSleepHours = () =>
getSleepHours(‘Monday’)+
getSleepHours(‘Tuesday’)+
getSleepHours(‘Wednesday’)+
getSleepHours(‘Thursday’)+
getSleepHours(‘Friday’)+
getSleepHours(‘Saturday’)+
getSleepHours(‘Sunday’);
//console.log(getActualSleepHours());

const getIdealSleepHours = (idealHours) => {
idealHours = 8;
return idealHours*7;
};
//console.log(getIdealSleepHours());

const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();

if(actualSleepHours===idealSleepHours){
console.log(You got the perfect amount of sleep!);
}else if(actualSleepHours>idealSleepHours){
console.log(You got ${actualSleepHours-idealSleepHours} more hours of sleep than you needed.);
}else{
console.log(You should get some rest. You slept ${idealSleepHours-actualSleepHours} less sleep than you needed.);
}
};

console.log(calculateSleepDebt());

This is logged:
You got the perfect amount of sleep!
undefined


#2

undefined is the status of the return value from the console.log() method. Since it has no return statement, it follows there is no return value. Looking at the code, you are logging a console.log() statement, thus logging its return (or lack of one), undefined.

Make the call to calculateSleepDebt without logging the return.


#3

THANK YOU!!! Not only did that work, but you helped me understand it better. Thank you, thank you, thank you.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.