Sleep Debt exercise 'undefined' - help please!

Hi, I’m looking for some help on the Sleep Debt project in the Learn JavaScript course (https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator).

I’ve finished the exercise and it works, which I’m very happy with because I struggled like mad with the ‘Rock, paper, scissors’ exercise :laughing:

But as well as my intended output (one of three console.log() statements depending on the figures) I also get ‘undefined’ showing up in the console. I’ve looked through my code but I can’t find the problem, can anyone help?

Here’s the code:

//Totaling up actual sleep hours for the week const getActualSleepHours = () => 6.5 + 7 + 2.5 + 4.5 + 10 + 7 + 7; //Calculating ideal sleep hours for the week const getIdealSleepHours = idealHours => idealHours * 7; //Calculating difference between actual hours and ideal hours for the week const calculateSleepDebt = () => { let actualSleepHours = getActualSleepHours(); let idealSleepHours = getIdealSleepHours(7); if (actualSleepHours === idealSleepHours) { console.log('Great, you got the perfect amount of sleep this week!'); } else if (actualSleepHours > idealSleepHours) { console.log(`You got ${actualSleepHours - idealSleepHours} hours more than you needed this week. Set that alarm earlier you lazy sod!`); } else { console.log(`You could have done with ${idealSleepHours - actualSleepHours} hours more sleep this week. Go and make a brew, and get an early night tonight.`) } } console.log(calculateSleepDebt());

Wouldn’t you know it, I worked it out two minutes after posting here. So in case anyone else sees this when they have the same issue, here’s what it was:

When I call calculateSleepDebt() right at the end, I didn’t need to call it inside a console.log() statement. I was doing this because previously I’d struggled to see the output of my functions in order to test them, but that’s because they just return a value. This function already contains the console.log() for each scenario, so adding another one was overkill.

1 Like

That is correct. You’d only use a separate console.log() to get the state of a variable at a certain point in time or to log the return of a function.

1 Like