NaN problem

Hi, everyone.
As the title says, i have a problem with the Sleep Debt Calculator in a JS lesson.
When i run it, it writes “Nan” instead of the hours.
I usually find my mistakes with “Unstuck” or pasting my code in Visual Code but there i cannot find it.
Please help me.

Here’s the code :

const getSleepHours = day => {
  switch(day) {
    case 'Monday': 
      return 10;
      break;
    case 'Tuesday':
      return 9;
      break;
    case 'Wednesday':
      return 8;
      break;
    case 'Thursday':
      return 7;
      break;
    case 'Friday':
      return 6;
      break;
    case 'Saturday':
      return 11;
      break;
    case 'Sunday':
    return 12;
    break;
  }
};

const getActualSleepHours = () => {
  getSleepHours('Monday') + 
  getSleepHours('Tuesday') + 
  getSleepHours('Wednesday') + 
  getSleepHours('Thursday') + 
  getSleepHours('Friday') + 
  getSleepHours('Saturday') + 
  getSleepHours('Sunday')
  };

const getIdealSleepHours = () => {
 const idealHours = 8;
 return idealHours * 7;
};

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

if (actualSleepHours === idealSleepHours) {
  console.log('You got a perfect amount of sleep.');
} else if (actualSleepHours > idealSleepHours) {
  console.log('You have '+ (actualSleepHours - idealSleepHours) + ' too much hour(s) of sleep.');
} else {
  console.log('You need ' + (idealSleepHours - actualSleepHours) + ' more hour(s) of sleep.')
};
};

calculateSleepDebt();

the first step to debug would be to log the values you use in the equation:

  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  console.log(actualSleepHours, idealSleepHours);

where we can see actualSleepHours is undefined, that is because nothing is returned by the getActualSleepHours function

1 Like

I checked with your tip, i read almost all the topics on the Sleep Debt Calculator and it finished with a missplaced ending bracket. That’s so great to finally find the bug !!!

Thanks for the tips.

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