Why am I getting an 'undefined' from my Sleep Debt Calculator?

Ref.: [Sleep Debt Calculator](https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-22-javascript-syntax-part-i/modules/wdcp-22-learn-javascript-syntax-functions/projects/sleep-debt-calculator](https://Sleep Debt Calculator)

I managed to get the right output for this exercise, but an ‘undefined’ appears after I call the function. Could any of our more experienced learners help me figure out why? Thanks in advance!

Here’s my code:

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 8; break; case 'sunday': return 9; break; default: return 7; } }; const getActualSleepHours = () => getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday'); getIdealSleepHours = () => { const idealHours = 7; return idealHours * 7; } calculateSleepDebt = () => { const actualSleepHours = getActualSleepHours(); const idealSleepHours = getIdealSleepHours(); if (actualSleepHours > idealSleepHours) { console.log('You got too much sleep: ' + (actualSleepHours - idealSleepHours) + ' more hours than needed.'); } else if (actualSleepHours < idealSleepHours) { console.log('You got too little sleep: ' + (idealSleepHours - actualSleepHours) + ' less hours than needed.'); } else { console.log('You got just enough sleep.'); } } console.log(calculateSleepDebt());

Hi! I am not an experienced learner but I think you need to just write

calculateSleepDebt()

instead of putting it inside the console.log().

1 Like

Hi try to remove from your last function “console.log()” and instead write return

calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();
const idealSleepHours = getIdealSleepHours();
if (actualSleepHours > idealSleepHours) {
return ‘You got too much sleep: ’ + (actualSleepHours - idealSleepHours) + ’ more hours than needed.’;
} else if (actualSleepHours < idealSleepHours) {
return ‘You got too little sleep: ’ + (idealSleepHours - actualSleepHours) + ’ less hours than needed.’;
} else {
return ‘You got just enough sleep.’;
}

Good luck!

1 Like

Removing console.log() worked! Thanks :hugs:

The problem was apparently in calling the function with a console.log statement. Thanks for your help!