Sleep calc, getting NaN

So I’m going through the sleep debt calc project and I cannot figure out why console.log(getActualSleepHours()); is logging NaN. console.log(getSleepHours(‘monday’)); returns an 8 so why does adding them all up not work and return NaN? Here is my code.

const getSleepHours = day => {
  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 8
      break;
    case 'saturday':
      return 8
      break;
    case 'sunday':
      return 8
      break;
  }
}
const getActualSleepHours = () => { 
  return getSleepHours('monday') +  
  getSleepHours('tuesday')+
  getSleepHours('wednesday')+
  getSleepHours('thursday')+ 
  getSleepHours('friday')+ 
  getSleepHours('Saturday')+ 
  getSleepHours('Sunday');
}

function getIdealSleepHours() {
  let idealHours = 8;
  return idealHours * 7;
}
console.log(getActualSleepHours())
2 Likes

this code is so error prone, if day is not the switch statement, the consequences can be quite confusing. Maybe you should handle it?

just for a quick fix, you could add a default to see which day is not in the switch statment

3 Likes

One little hint: Javascript is “case sensitive” :wink: . Hope this helps.

3 Likes

It did omg i capitalized saturday and sunday out of habit. Works fine now. ty ty.

2 Likes

As @janneslohmeijer said, you have to use sunday instead of Sunday.

In this case users also would write it with big or small letters so you can use:

day = day.toLowerCase

before the switch statement

Hope this helps :grinning:

2 Likes