SleepDebtCalculator

Hi! Consider my coding knowledge equal to almost nothing, as I am very new to this. I was tugging away at the sleepdebt exercise, and when writing up the last if/else statements I keep getting this error message: ReferenceError: actualSleepHours is not defined
at Object.
I have no idea why. I checked the hints, and it seems I have done everything right (obviously not, though).
Help is greatly appreciated.
Here’s my code so far (https://gist.github.com/b5c0cb7056476b5b3f2e8ef2917babb1) :

const getSleepHours = day => {
  switch (day) {
    case 'monday':
      return 8;
    case 'tuesday':
      return 7;
    case 'wednesday':
      return 6;
    case 'thursday':
      return 5;
    case 'friday':
      return 6;
    case 'saturday':
      return 7;
    case 'sunday':
      return 7,5;
  }
}
const getActualSleepHours = () => 
  getSleepHours('monday') + getSleepHours('tuesday') + getSleepHours('wednesday') + getSleepHours('thursday') + getSleepHours('friday') + getSleepHours('saturday') + getSleepHours('sunday');


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

const calculateSleepDept = () => {
  let actualSleepHours = getActualSleepHours();
  let idealSleepHours = getIdealSleepHours();
};

if (actualSleepHours === idealSleepHours) {
  console.log('You da king of sleep my man! High five!');
} else if (actualSleepHours > idealSleepHours) {
  console.log('Sleep less!');
} else {
  console.log('Sleep more!');
}

here you have the function:


const calculateSleepDept = () => {
  let actualSleepHours = getActualSleepHours();
  let idealSleepHours = getIdealSleepHours();
};

in which you define actualSleepHours and idealSleepHours variables, which now only exists within the function (scope rules), then after/outside the function:

if (actualSleepHours === idealSleepHours) {
  console.log('You da king of sleep my man! High five!');
} else if (actualSleepHours > idealSleepHours) {
  console.log('Sleep less!');
} else {
  console.log('Sleep more!');
}

you attempt to use these variables (while not in the scope)

what should change here?

Ah, I should put the if/else inside the function as well?
Seems to work now! Thanks a lot! You made a newbie’s day a little easier :slight_smile:

Or maybe not. I ran into the same error message after I put the if/else within the function and tried to run the calculateSleepDebt() function afterwards, as described in the exercise:

const calculateSleepDept = () => {
  let actualSleepHours = getActualSleepHours();
  let idealSleepHours = getIdealSleepHours();
if (actualSleepHours === idealSleepHours) {
  console.log('Slept good! You got' + idealHours + ' hours of sleep last night.');
} else if (actualSleepHours > idealSleepHours) {
  console.log('You overslept with' + (actualSleepHours - idealSleepHours) + ' hours last night.');
} else {
  console.log('Sleep more! You slept' + (idealSleepHours - actualSleepHours) + ' hours less than ideal last night.');
}
};
calculateSleepDebt();

The code above this has remained unchanged.

The error is different, first it had a problem with actualSleepHours, now it has a problem with something else. If i look at the function definition:

const calculateSleepDept = () => {

and then the call:

calculateSleepDebt();

i see the typo/spelling mistake

Yeah, my bad. Thanks again!

That is okay, making mistakes is part of learning. Now next time you see an error like this, hopefully you also consider checking for typos/spelling :slight_smile:

Yeah, I will definitely do that from now on. This one was a bit embarrassing :stuck_out_tongue:

1 Like