Stuck with Sleep Debt calculator

This my code:

function getSleepHours(day){
  day = day.toLowerCase()
  switch (day){
  case 'monday':
  return 4;
  break;
  case 'tuesday':
  return 7;
  break;
  case 'wednesday':
  return 8;
  break;
  case 'thursday':
  return 3;
  break;
  case 'friday':
  return 9;
  break;
  case 'saturday':
  return 5;
  break;
  case 'sunday':
  return 5;
  break;
  }
}

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

const getIdealSleepHours = () => {
  let idealHours = 5
  return idealHours * 7
}

function calculateSleepDebt(){
  let actualSleepHours = getActualSleepHours
  let idealSleepHours = getIdealSleepHours
  if (actualSleepHours === idealSleepHours){
    console.log('Perfect amount of sleep achieved!')
    console.log(`You got the same hours of sleep as your ideal hours.`)
    return `Your target was ${idealSleepHours} of sleep.`
  } else if (actualSleepHours > idealSleepHours){
    console.log('You got more than your ideal needed sleep hours!')
    console.log(`You got ` + (actualSleepHours - idealSleepHours) ` more hours than your ideal`)
  } else if (actualSleepHours < idealSleepHours){
    console.log('You got less than the your ideal sleep hours!')
    console.log('You got ' + (idealSleepHours - actualSleepHours) + ' less hours than your ideal sleep hours')
  }
}

console.log(calculateSleepDebt())

Confused as to why it isn’t working. I understand that the end is messy but It should at least execute the right if then statement but its not. help!

Hello @majorh, welcome to the forums! There are two main things that are happening with your code. FIrstly, you are getting Nan. This is caused becasue you don’t call the function which retuns the number of sleep hours:

This is the syntax for calling a function, and assigning it to a variable:

let someVar = someFunction()

This is what you did:

Can you see the difference? What you’re doing actually assigns the memory id (for lack of a better word) for the function-which is why you get NaN when you try (idealSleepHours - actualSleepHours).


Secondly, the undefined at the end of the output shouldn’t be there. That is a result you the calculateSleepDebt function not returning anything. You use console.log() on the function call, which means you are printing the value returned from the function. However, since there is no return value, it is automatically assigned to undefined.

I hope this helps!

Thank you. How much coding experience do you have with JavaScript?

Me? Not too much-I only know the fundamentals; I prefer Python.

1 Like