Debug Sleep Debt Calculator Project

Hi Everyone!

In regards to the Sleep Debt Calculator ( https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/sleep-debt-calculator ) project in the Intro to JavaScript, I can’t seem to debug my code to get it to work. I feel I am overlooking the error and it may be right under my nose, so hoping to get a second pair of eyes on it. I have gone through the step-by-step video as well.

The error keeps coming up as actualSleepHours is not defined (I’ve attached a screenshot). I am trying to rack my brain as to where and how else I should be defining it. See below for my code. If you see anything else that could use cleaning up (spacing is a bit warped pasting into the forum) or where there are errors, please don’t hesitate to let me know!:

const getSleepHours = day => {
if (day ===‘Sunday’) {
return 8;
} else if (day === ‘Monday’) {
return 6;}
else if (day === ‘Tuesday’) {
return 6;}
else if (day === ‘Wednesday’) {
return 7;}
else if (day === ‘Thursday’) {
return 8;}
else if (day === ‘Friday’) {
return 9;}
else if (day === ‘Saturday’) {return 6;}
else {return ‘Please enter a valid day of the week.’;}
};

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

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

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

if(actualSleepHours === idealSleepHours)
{
console.log(‘Congrats! You are getting ’ + actualSleepHours + ’ hours of sleep per week which is the perfect amount.’);
}
else if (actualSleepHours >
idealSleepHours) {
console.log(‘Congrats! You are getting ’ + (actualSleepHours - idealSleepHours) + ’ more hours than the ideal amount of sleep per week.’);
}
else {console.log(‘Looks like you need more rest! You are getting ’ + (idealSleepHours - actualSleepHours) + ’ less hours than the ideal amount of sleep per week.’);}
;

calculateSleepDebt();

``

Hello, @daniellelaurbaker, and welcome to the forums.

In the scope of your if statement included in the error message, where is actualSleepHours defined?

Hi!

I thought it was defined in the code below as equal to whatever getActualSleepHours(); 's value was. Would I define this elsewhere?

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

And if this is the case, why is it not bringing up an error saying that idealSleepHours is undefined?

Because the first undefined variable stopped the interpreter from continuing.

Your issue is one of scope. The variable actualSleepHours is not available (not defined) in the if statement. A variable declared inside a function is only available inside the function (unless it’s declared globally [Don’t Do That]). Your code, overall, lacks indentation which makes seeing what should go where a little more difficult.

const someFunction = () => { //opening brace for function body
  const someVariable = 'who cares';
}; //closing brace for function body

console.log(someVariable); // ReferenceError: someVariable is not defined

Hey @midlindner ! Thank you so much for your quick response. When I went to go and copy/paste my code from my editor it got a bit warped so some indentation is off. This project aside, do I still follow the double space or tab indentation I used in HTML/CSS for better readability in JavaScript?

And I think I understand now… thank you! I ended up defining both actualSleepHours and idealSleepHours and it is working now. Would you mind taking a look through my new code and see if this is what you meant?


const getSleepHours = day => { 
 if (day ==='Sunday') {return 8; 
 } else if (day === 'Monday') {
   return 6;} 
   else if (day === 'Tuesday') {
     return 6;} 
     else if (day === 'Wednesday') {
       return 7;} 
       else if (day === 'Thursday') {
         return 8;} 
         else if (day === 'Friday') {
           return 9;} 
           else if (day === 'Saturday') {return 6;}
           else {return 'Please enter a valid day of the week.';}
};

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


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

let actualSleepHours = getActualSleepHours();
let idealSleepHours = getIdealSleepHours();

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


if(actualSleepHours === idealSleepHours)
{
  console.log('Congrats! You are getting ' + actualSleepHours + ' hours of sleep per week which is the perfect amount.');
} else if (actualSleepHours > 
idealSleepHours) {
  console.log('Congrats! You are getting ' + (actualSleepHours - idealSleepHours) + ' more hours than the ideal amount of sleep per week.');
} else {console.log('Looks like you need more rest! You are getting ' + (idealSleepHours - actualSleepHours) + ' less hours than the ideal amount of sleep per week.');}
;

calculateSleepDebt();

Um… No. That’s not it. You didn’t need to do this:

You just need this:

to be inside this function:

The way you did it, calculateSleepDebt is no longer being used. It’s called, assigns values to 2 locally scoped variables that have no purpose, and it’s done.

Double space indentation is commonly used in JavaScript.
This:

should look something like this:

const getSleepHours = day => { 
  if (day ==='Sunday') {
    return 8; 
  } else if (day === 'Monday') {
    return 6;
  } else if (day === 'Tuesday') {
    return 6;
  } else if (day === 'Wednesday') {
    return 7;
  } else if (day === 'Thursday') {
    return 8;
  } else if (day === 'Friday') {
    return 9;
  } else if (day === 'Saturday') {
    return 6;
  } else {
    return 'Please enter a valid day of the week.';
  }
};