ReferenceError: Sleep Debt Calculator


#1

Hi,

I have reference error on my code and I could not identify what is wrong. I re-wrote it loads of times, found other users with similar issues, but my code still has the same error.
It is saying:
if (actualSleepHours === idealSleepHours) {
^
ReferenceError: actualSleepHours is not defined
at Object.

The code is

const getSleepHours = day => {
  if (day === 'monday') {
    return 8;
  } else if (day === 'tuesday') {
    return 8;         
  } else if (day === 'wednesday') {
    return 8;         
  } else if (day === 'thursday') {
    return 8;         
  } else if (day === 'friday') {
    return 8;         
  } else if (day === 'saturday') {
    return 8;         
  } else if (day === 'sunday') {
    return 8;         
  }
};

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

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


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

if (actualSleepHours === idealSleepHours) {
  console.log('You have the perfect amount of sleep');
} else if (actualSleepHours > idealSleepHours) {
  console.log('You slept a lot!!!');
} else {
  console.log('Stop now and go to bed');
}

Why it is saying that ‘actualSleepHours is not defined’?

Thank you


#2

actualSleepHours is defined, but within calculateSleepDebt function. So actualSleepHours only exist within function (scope), so the question is: what needs to change?


#3

Hey dude.
How does it is possible? A simple }; positioned incorrectly causes a disgrace on my code haha.
Man, you have eagle eyes. Thank you for your help. After I understand what you said, I could finish the exercise in minutes.
But, I didn’t understand the correct way to use the brackets {} on the functions. Sometimes there are {} and sometimes not. Why is that?

Looks like this:

const getSleepHours = day => {
  if (day === 'monday') {
    return 8;
  } else if (day === 'tuesday') {
    return 8;         
  } else if (day === 'wednesday') {
    return 8;         
  } else if (day === 'thursday') {
    return 8;         
  } else if (day === 'friday') {
    return 8;         
  } else if (day === 'saturday') {
    return 8;         
  } else if (day === 'sunday') {
    return 9;         
  }
};

const getActualSleepHours = () => 6 + 5 + 6 + 4 + 5 + 6;

/*getSleepHours('monday') +
  getSleepHours('tuesday') +
  getSleepHours('wednesday') +
  getSleepHours('thursday') +
  getSleepHours('friday') +
  getSleepHours('saturday') +
  getSleepHours('sunday');*/

const getIdealSleepHours = idealHours => 
  idealHours * 7;

const calculateSleepDebt = () => {
    const actualSleepHours = getActualSleepHours();
    const idealSleepHours = getIdealSleepHours(8);
if (actualSleepHours === idealSleepHours) {
  console.log('You have the perfect amount of sleep');
} else if (actualSleepHours > idealSleepHours) {
  console.log(`You got ${actualSleepHours - idealSleepHours} extra hours sleeping. Shame!!!`);
} else {
  console.log(`You got ${idealSleepHours - actualSleepHours} hours. You are working too hard. Get some rest!!!`);
}
};

calculateSleepDebt()

Thanks mate!


#4

yep, welcome to the wonderful world of programming. Please enjoy our stay :stuck_out_tongue:

But i just nudged you in the right direction, you figured it out and understood it. That is certainly worth something :wink:

from MDN (link):

(param1, param2, …, paramN) => expression
// equivalent to: => { return expression; } 

we can omit curly brackets and return keyword when the function only has a single expression, not a statement (multiple lines of code within the function). Its just a shorthand. You don’t have to use this shorthand if you do not feel comfortable with it


#5

Hey stetim94,

Yes, it was cool from you. When I saw your replay first, I was really pissed off, because I couldn’t identify the issue anyway. But after I understand it, I thought: ‘this guy is really smart!’. It made me understand the issue by the root. Thank you man!

Now, I will keep going deep into the programming world.


#6

A rather ironic thing, the less i help you the better. Then the puzzle pieces will hopefully come together.

But of course i would help you further if you needed it :slight_smile:


#7

Thank you mate. It makes total sense!


#8

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