Sleep Debt calculator help


#1

Hello i am currently stuck on this task

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

I cannot understand where I am going wrong and any help greatly appreciated. Thanks.

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

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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  if (actualSleepHours === idealSleepHours) {
  console.log('You got a perfect amount of sleep');
  }
  if (actualSleepHours < idealSleepHours)
    {
      console.log('You got ' (idealSleepHours - actualSleepHours) +  ' Get some rest, maybe take a nap');
    }
  if (actualSleepHours > idealSleepHours)
    {
      console.log('You got ' + (actualSleepHours - idealSleepHours) + 'thats more than enough sleep');
    }
  const idealSleepHours = getIdealSleepHours();
};


calculateSleepDebt();

#2

here:

if (actualSleepHours === idealSleepHours)

ReferenceError: idealSleepHours is not defined

which is pretty self explaining? idealSleepHours is undefined, as such it can’t be used in a comparison. undefined variable could also mean out of scope, that is possible as well


#3

“wrong” is a difference between what you expected and what you saw

for future reference, you should explain what those are
a question should convince the reader that you can’t solve it and help the reader understand what you’re missing


#4

Thanks for responding, I am unsure as to move on still.
How do I define idealSleepHours? or could you give an example of how you define something so that I can have go :slight_smile:


#5

you have a function capable of calculating the ideal number of hours sleep required? So, you can use this function to define a variable idealSleepHours in a very similair fashion as actualSleepHours


#6

Ok, thanks I will make sure to explain exactly why is is not responding in the way i expected.
I am very new to this and find it hard to even articulate how I expect things to work… :thinking:


#7

Thanks again, I think I have solved that but I am getting nothing when I run the code?!?!

Here is my code:

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

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

const calculateSleepDebt = () => {
  const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  if (actualSleepHours === idealSleepHours) {
  console.log('You got a perfect amount of sleep');
  }
  if (actualSleepHours < idealSleepHours)
    {
      console.log('You got ' (idealSleepHours - actualSleepHours) +  ' Get some rest, maybe take a nap');
    }
  if (actualSleepHours > idealSleepHours)
    {
      console.log('You got ' + (actualSleepHours - idealSleepHours) + 'thats more than enough sleep');
    }
  
};


calculateSleepDebt();

I am finding this really tough going… I really appreciate the help and will take a couple of hours away then come back to it.


#8

i added a console.log:

const actualSleepHours = getActualSleepHours();
  const idealSleepHours = getIdealSleepHours();
  console.log(actualSleepHours, idealSleepHours)

actualSleepHours is a function, it should be an integer. Somewhere, you go wrong


#9

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