Sleep Debt Project - Task 12 returns Nan, why?

Howdy All,
The SleepDebt project works well until Task 12 bullet point 2 is substituted with the code contained in the “Get a Hint” section.

At the end of my code, there are 3 console.log outputs:
1/ Message generated by actual sleep compared to ideal sleep.
2/ Number of actual sleep hours.
3/ Number of ideal sleep hours.

Before the Task 12.2 substitution the outputs are:
“You have the perfect amount of sleep”
“56”
“56”

After the Task 12.2 substitution the outputs are:
“You have the perfect amount of sleep”
“56”
“NaN”

Please, can anyone see the error of my code and explain why this is happening?
Thank you kindly

const getSleepHours = day => {
day = day.toLowerCase();
switch(day){
case ‘monday’:
return 8
break;
case ‘tuesday’:
return 8
break;
case ‘wednesday’:
return 8
break;
case ‘thursday’:
return 8
break;
case ‘friday’:
return 7
break;
case ‘saturday’:
return 8
break;
case ‘sunday’:
return 8
break;

  default:
  return "Error!, please enter a day of the week."

}
};

const getActualSleepHours = () =>
8 + 8 + 8 + 8 + 8 + 8 + 8;
// above & below are alternative input styles
/*
getSleepHours(‘monday’) +
getSleepHours(‘tuesday’) +
getSleepHours(‘wednesday’) +
getSleepHours(‘thursday’) +
getSleepHours(‘friday’) +
getSleepHours(‘saturday’) +
getSleepHours(‘sunday’);
/
const getIdealSleepHours = () => {
const idealHours = 8;
return idealHours * 7;
};
/

// version2 of getIdealSleepHours
const getIdealSleepHours = idealHours => idealHours * 7;

}
*/
const calculateSleepDebt = () => {
const actualSleepHours = getActualSleepHours();

const idealSleepHours = getIdealSleepHours(8);

if (actualSleepHours === idealSleepHours)
{
console.log(“You have the perfect amount of sleep.”);

} else if (idealSleepHours > actualSleepHours)
{
console.log(You need

  • (idealSleepHours - actualSleepHours)
  • hour(s) more rest each week.);

}else if (idealSleepHours < actualSleepHours)
{
console.log(“You are well rested.”);
}
else{
console.log(“Check your code”)
}
};

calculateSleepDebt();

console.log(getActualSleepHours());

console.log(getIdealSleepHours());

Hello @peterjackson108 , welcome to the forums! Could you please post your code according to these guidelines?


When you call getIdealSleepHours():

You don’t use a parameter, yet the function takes one parameter:

That means the compiler automatically assigns idealHours to undefined. And undefined * 7 is not a number (NaN).

Hello codeneutrino,

Well done! - thank you for your helpful reply.

This is my first post/question to the forum, I will take note of the guidelines for future posts.

Your answer makes perfect sense.
The idealHours function in

  const getIdealSleepHours = idealHours => ***idealHours*** * 7;

needed the parameter ‘idealHours’ to be provided the argument of ‘8’ placed in the parentheses.
Like so: console.log(getIdealSleepHours(8));
`
I am learning it takes time and practice to progress from seeing a forest of words and symbols to seeing the individual groups of trees and how they relate to each other.
Thank you for your guidance!

1 Like