Help with "Sleep Dept Calculator" Step 12

Source:

Source: https://www.codecademy.com/courses/introduction-to-javascript/projects/sleep-debt-calculator

I am a super newbie. Like really really new. I don’t know what I am doing wrong. I am getting a “NaN” as in the console. I know it’s an easy fix.

Can someone tell me where i messed up?

My code is below:


const getSleepHours = day => {

// results of days and hours
switch(day) {
  case 'monday' :
    	return 8
    break
  case 'tuesday':
    	return 9
    break
  case 'wednesday':
    	return 8
    break
  case 'thursday':
    	return 7
    break
  case 'friday':
    	return 8
    break
  case 'saturday':
    	return 8
    break
  case 'sunday':
    	return 7
    break
  default:
    return 'Invalid Input'
}    
    
}

const getActualSleepHours = () => 6 + 7 + 9 + 8 + 5 + 10 + 11;

const getIdealSleepHours = idealHours => idealHours * 7;

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours(8);
  if (actualSleepHours === idealSleepHours) {
    console.log('PERFECT!');
  } else if (actualSleepHours > idealSleepHours) {
    console.log('TOO MUCH SLEEP.You got ' + ( -  idealSleepHours) + ' hour(s) LESS sleep than you need this week.' );    
  } else if (actualSleepHours < idealSleepHours) {
    console.log('Go To Bed!!! You got ' + (idealSleepHours - actualSleepHours) + ' hour(s) LESS sleep than you need this week.' );
  } else {
    console.log('ERROR!')
  }   
}

calculateSleepDebt ()

console.log ('You get ' + (getSleepHours ('monday')) + ' hours of sleep on this day')
console.log ('You get ' + (getActualSleepHours()) + ' hours per week')
console.log ('Ideal Sleep Hours are ' + (getIdealSleepHours()) + ' per week')

~~~~~

What are the ideal sleep hours?

Look at your last line.

I’ve looked, and I don’t know what’s wrong

Have a look at where you’ve called getIdealSleepHours() in calculateSleepDebt.

You then call getIdealSleepHours() again in the last line of your code. But those two calls are different… first time you call it, it’s correct.

1 Like

So

console.log (‘Ideal Sleep Hours are ’ + (getIdealSleepHours()) + ’ per week’)

should be

console.log (‘Ideal Sleep Hours are ’ + (getIdealSleepHours(8)) + ’ per week’)

But i could put any number there and it would change. Shouldn’t it calculate it automatically?

Sorry for the really stupid questions. I am so new to this, and really a, trying to learn on my own. It’s not very easy for me.

That function does calculate the weekly ideal sleep hours, i.e. 8 * 7. It needs an input of the preferred nightly sleep hours (8). Just checked my code for that exercise, that’s how mine is too.

1 Like
const getActualSleepHours = () => 6 + 7 + 9 + 8 + 5 + 10 + 11;

This works, but you aren’t using the getSleepHours([day]) function as called for in the project. Your code is returning the sum of the numbers you typed in instead of the sum of the numbers listed in your getSleepHours([day]) function. This will get the hours from your function:
const getActualSleepHours = () => getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');

Also a couple of observations:
(1) You should have a ; at the end of each statement. For example return 8;

(2) Many environments will give you an error or a warning about having an unreachable break in your switch statement. When you use return, the break will never be reached. There are 2 possible fixes. It’s arguable as to which one is “best practice.” One way is to simply delete all of the breaks.
The other would be to replace the returns with an assignment to a variable, and then return the value of the variable after the switch has executed. For example:

    day = day.toLowerCase();
    switch (day) {
    case 'monday':
      return 8;
    case 'tuesday':
      return 8;
    case 'wednesday':
      return 9;
    case 'thursday':
      return 10;
    case 'friday':
      return 8;
    case 'saturday':
      return 9;
    case 'sunday':
      return 5;
    default:
      return null;
    }

OR:

  day = day.toLowerCase();
  let hours;
  switch (day) {
    case 'monday':
      hours = 8;
    break;
    case 'tuesday':
      hours = 8;
    break;
    case 'wednesday':
      hours = 9;
    break;
    case 'thursday':
      hours = 10;
    break;
    case 'friday':
      hours = 8;
    break;
    case 'saturday':
      hours = 9;
    break;
    case 'sunday':
      hours = 5;
    break;
    default:
      hours = null;
    break;
  }
  return hours;

(3) While calculateSleepDebt () works it really should be calculateSleepDebt(); to conform to standards.
Hopefully you find this helpful. Good luck with your project!

1 Like