Help with SleepDebt Calculator

Hey,
I’m doing the Web Development course and I’m stuck on the Sleep Debt Calculator. I’m new with JavaScript and I really can’t get any further. When I click on save I get:
8
52.5
You got NaN hour(s) sleep. What is wrong with you go back to bed!

The entire script look like this:

const getSleepHours = day => {

switch (day) {
case 'monday':
return 8
break;
case 'tuesday':
return 5
break;
case 'wednesday':
return 7
break;
case 'thursday':
return 7
break;
case 'friday':
return 7
break;
case 'saturday':
return 9
break;
case 'sunday':
return 9
}
};

const getActualSleepHours = () => {
 getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');
};

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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours ();
    if (actualSleepHours === idealSleepHours) {
      console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) less sleep than you needed this week. Get some rest.');

      } else if (actualSleepHours < idealSleepHours) {
      console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) sleep that you really needed. Enjoy the day.');
      } else {
      console.log('You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) sleep. What is wrong with you go back to bed!');
      }
};

console.log(getSleepHours('monday'));
console.log(getIdealSleepHours());

calculateSleepDebt();

I appreciate any type of help I can get :blush:

Best

1 Like

The first question you should ask yourself is what could give NaN? Think about it for a second before continue reading.

NaN can occur when we try to do a math operation on something that isn’t a number, like:

console.log("abc" - 3)

then the next logical step would to inspect your variable you use in the math operation which gives NaN:

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

as you can see from the log, getActualSleepHours gives undefined. Can you take it from here?

I got it to work, thanks :slight_smile:

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