Sleep Debt Calculator


#1

I’m having some difficulty working out the issue from my code below.

It’s flagging up this line:

{return (ideaHours * 7);} ^ ReferenceError: ideaHours is not defined

const getSleepHours = (day) => {
  switch(day){
    case 'Monday':
      return 8;
    case 'Tuesday':
      return 7;
    case 'Wednesday':
      return 7;
    case 'Thursday':
      return 6;
    case 'Friday':
      return 5;
    case 'Saturday':
      return 8;
    case 'Sunday':
      return 7;
    default:
      console.log('Error.');
};
}

const getActualSleepHours = () => {
  return getSleepHours('Monday') + getSleepHours ('Tuesday') + getSleepHours ('Wednesday') + getSleepHours ('Thursday') + getSleepHours ('Friday') + getSleepHours ('Saturday') + getSleepHours ('Sunday');
};

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

calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  
  if(actualSleepHours === ideaSleepHours){
    console.log('Well done, you got the perfect amount of sleep!')
  } else if (actualSleepHours > ideaSleepHours){
    console.log('You have over slept by' + (actualSleepHours - idealSleepHours) + 'hours');
  } else if (actualSleepHours < ideaSleepHours){
    console.log('You only slept' + (actualSleepHours - ideaSleepHours) + 'You have not slept enough');}
};

calculateSleepDebt();

I’ve looked and tried to diagnose the issue, but what ever I try doesn’t seem to fix this line.

Link to course: https://www.codecademy.com/courses/learn-javascript-functions/projects/sleep-debt-calculator?course_redirect=introduction-to-javascript


#2

Hi @methodplayer93805,

You have a spelling error :wink:


Refer the error:

ReferenceError: ideaHours is not defined

Because ideaHours is not yet/never defined in your code.

The problem is here:

{return (ideaHours * 7);}

Missing a “l” letter, should refer to idealHours?


Hope it helps, cheers! :slight_smile:


#3

Lmao - Oh, that’s embarrassing - thank you!


#4

Typos like this happen to the best of us, so I wouldn’t be too embarrassed.


#5

@methodplayer93805,

Just tested your code,

It seemed you have several spelling errors related to ideal in idealSleepHours, see if you can catch them all.

:slight_smile:


#6

You wouldn’t believe the amount of head scratching I did. I was too busy looking at the rest of the code, seeing if I had missed something and completely overlooked something like spelling. Thank you.


#7

Thank codexthon!

I think I’ve snagged them all now! :slight_smile:

For some reason it’s returning that I had -8 hours sleep?


#8

Not at all pal, it’s so easy to overlook typos, happens to all of us. :slight_smile:

Suggestion here:

} else if (actualSleepHours > idealSleepHours){
    console.log('You have over slept by' + (actualSleepHours - idealSleepHours) + 'hours');
                                       ^                                          ^
} else if (actualSleepHours < idealSleepHours){
    console.log('You only slept' + (actualSleepHours - idealSleepHours) + 'You have not slept enough');}
                               ^                                       ^(+ " hours ")? missing?

Add additional empty space inside your strings for it to print nicely. :slight_smile:


#9

Here:

else if (actualSleepHours < idealSleepHours){
    console.log('You only slept ' + (actualSleepHours - idealSleepHours) + ' hours, you have not slept enough');}
};

If your actualSleepHours is lesser than idealSleepHours, you should invert the minus operation so it doesn’t reach a negative value.

(idealSleepHours - actualSleepHours) should solve your problem.


#10

To add to what @codexthon is saying, It may, also, have something to do with the white-space in your getActualSleepHours function.

You currently have:

const getActualSleepHours = () => {
    return getSleepHours('Monday') + getSleepHours ('Tuesday') + getSleepHours ('Wednesday') + getSleepHours ('Thursday') + getSleepHours ('Friday') + getSleepHours ('Saturday') + getSleepHours ('Sunday');
};

You have written getSleepHours properly for Monday (i.e. getSleepHours('Monday')), but proceed to improperly write getSleepHours for Tuesday-Sunday.

For Tuesday - Sunday you have written getSleepHours like this:

getSleepHours ('Tuesday')

You can fix this by removing that space between “getSleepHours” and “(


Edit: Then again, It’s entirely possible that your “improper” use of whitespace is only “improper” in my head…


#11

Ah ha! Simple maths hey? Thanks for that! You’re a great help! :slight_smile:


#12

Thanks for the feedback @aquaphoenix17 - I’ll amend this :slight_smile:

I’m chuffed to bits with this. Thanks guys - you made the dream come true!

Please see my final code, any further criticism and anyway to tidy this code up is most welcome.

const getSleepHours = (day) => {
  switch(day){
    case 'Monday':
      return 8;
    case 'Tuesday':
      return 7;
    case 'Wednesday':
      return 7;
    case 'Thursday':
      return 6;
    case 'Friday':
      return 5;
    case 'Saturday':
      return 8;
    case 'Sunday':
      return 7;
    default:
      console.log('Error.');
};
}

const getActualSleepHours = () => {
  return getSleepHours('Monday') + getSleepHours('Tuesday') + getSleepHours('Wednesday') + getSleepHours('Thursday') + getSleepHours('Friday') + getSleepHours('Saturday') + getSleepHours('Sunday');
};

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

calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  
  if(actualSleepHours === idealSleepHours){
    console.log('Well done, you got the perfect amount of sleep!')
  } else if (actualSleepHours > idealSleepHours){
    console.log('You have over slept by' + (actualSleepHours - idealSleepHours) + 'hours');
  } else if (actualSleepHours < idealSleepHours){
    console.log('You only slept ' + (idealSleepHours - actualSleepHours) + ' hours. You have not slept enough');}
};

calculateSleepDebt();


#13

it’s good practise to make sure this code is tight as possible! :slight_smile: It does look much better :wink:


#14

Just a bit suggestion, you can change the sentence into printing like this:

console.log('You slept ' + (idealSleepHours - actualSleepHours) + ' hours lesser than ideal hours, you have not slept enough')

So it printed more logically:

You slept 8 hours lesser than ideal hours, you have not slept enough

This statement sounded like you just had an 8 hours sleep. :wink:

You only slept 8 hours. You have not slept enough

Cheers :slight_smile:


#15

@aquaphoenix17 and @methodplayer93805,

Just want to state that the white space in between

getSleepHours   ('Monday')

doesn’t matter or changes the code when you code in real.

But somehow, it’s better to stick to getSleepHours('Monday') without the spaces, for two reasons:

  • It’s neater that way.
  • Secondly, sometimes the exercises’ SCT do not tolerate additional spaces, thus leading you not passing the exercises.

Hope that clarifies :slight_smile: Happy Coding!


#16

I know it doesn’t… that’s why I made this edit…

Also, not to be a stickler for grammar or anything, but if you want the statement to be logical, you would say “You have slept 8 hours less than the ideal amount,” as this is proper grammar.


#17

You’re right. This would make much more sense! Thank you again!


#18

You’re welcome @methodplayer93805 :smiley:

Oops, @aquaphoenix17, I overlooked that. :sweat_smile:


#20

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