JavaScript Syntax: Sleep Debt

Hi, I managed to get on okay with this project and all of the code works and adjusts when I change the daily hours. However, it always gives me an undefined at the end of the console no matter what output. Why is this undefined here?

// creating a function that takes how many hours I slept each day of the week
const getSleepHours = day => {
day = day.toLowerCase();
if (day === ‘monday’) {
return 9;
} else if (day === ‘tuesday’) {
return 4;
} else if (day === ‘wednesday’) {
return 13;
} else if (day === ‘thursday’) {
return 7;
} else if (day === ‘friday’) {
return 8;
} else if (day === ‘saturday’) {
return 8;
} else if (day === ‘sunday’) {
return 7;
}
};
// tests the function works
console.log(getSleepHours(‘SATURDAY’));
/* creating a function that takes how many hours I slept each day and calculates it in hours/week */
const getActualSleepHours = () => {
return (getSleepHours(‘monday’) + getSleepHours(‘tuesday’) + getSleepHours(‘wednesday’) + getSleepHours(‘thursday’) + getSleepHours(‘friday’) + getSleepHours (‘saturday’) + getSleepHours(‘sunday’));
};
// tests function works
console.log(getActualSleepHours());
// creating function of how many hours I want per night into hours/week
const getIdealSleepHours = () => {
let idealHours = 8;
return idealHours * 7;
}
// tests function works
console.log(getIdealSleepHours());
// creating function to calculate sleep debt
const calculateSleepDebt = () => {
// creates variables relating to the functions to be used in this function
let actualSleepHours = getActualSleepHours();
let idealSleepHours = getIdealSleepHours();
// if/else that reacts based on actualSleepHours compared to idealSleepHours
if (actualSleepHours === idealSleepHours){
console.log(‘You got the perfect amount of sleep!’);
} else if (actualSleepHours > idealSleepHours){
console.log(‘You got ’ + (actualSleepHours - idealSleepHours) + ’ hours more than needed!’);
} else if(actualSleepHours < idealSleepHours){
console.log(‘You need to get more sleep! You got ’ + (idealSleepHours - actualSleepHours) + ’ less than needed!’);
}

};
// calls final function to calculate sleep debt and give advice
console.log(calculateSleepDebt());

Console returns:
8
56
56
You got the perfect amount of sleep!
undefined

To preserve code formatting in forum posts, see: [How to] Format code in posts

Your calculateSleepDebt function is using console.log statements to print strings, but no value is being explicitly returned by the function. Hence, undefined is returned.

If you don’t want to see undefined, you can simply edit the following statement:

// Instead of:
console.log(calculateSleepDebt());
// "You got the perfect amount of sleep!"
// undefined

// Change to:
calculateSleepDebt();
// "You got the perfect amount of sleep!"
// (The function is still returning undefined but we don't see
// it being printed because of the omission of console.log)

Alternatively, you could do explicit returns from the function.

// You wrote:
const calculateSleepDebt = () => {
    ... // some code
    console.log("You got the perfect amount of sleep!");
    ... // more code
}

console.log(calculateSleepDebt());
// "You got the perfect amount of sleep!"
// undefined
// Change to:
const calculateSleepDebt = () => {
    ... // some code
    return "You got the perfect amount of sleep!";
    ... // more code
}

console.log(calculateSleepDebt());
// "You got the perfect amount of sleep!"

Ah I knew it would be a silly mistake like that. Thank you! So because I did console.log in the function I didn’t need to call the function with console.log outside of it? But if it was an explicit return I would need to?

Thanks a lot!

1 Like