Error Undefined

I am currently working on this project https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/learn-javascript-functions/projects/sleep-debt-calculator on the javascript course.
I thought that all my code was correct and then when I tried to run it, it says one of my functions is undefined, which it isn’t. I have gone through the explanation video and my code is EXACTLY the same as the one in the video yet still the message appears. I don’t want to move on until I can understand why/if it is wrong. PLEASE HELP.!

Screen Shot 2020-05-05 at 17.47.51|690x269

Hello, @tag4620208680, and welcome to the forums!

The gist of your problem is that in your if condition you are referring to something that is in fact undefined. Try printing the values immediately prior to your if statement to see for yourself. If the undefined variable should have been defined, where is that? Is the variable defined in the same scope that you are trying to access its value from?

Also, while screen shots are sometimes helpful, it is best to share your actual code when seeking help with code. Copy and paste it in a post following the guidelines found here: How do I format code in my posts?

1 Like

After viewing a very similar question regarding this project in another thread, I realize that scope has not yet been covered in the lessons. That will make finding your mistake a little more difficult. Your function has a block of code that executes when it is called. The block begins with { and ends with }. You have defined two new variables inside the function. Those variable are only accessible inside the function. Your if statement appears after the function’s closing brace }, so the variables are both undefined. You just need to move the if statement so that it is inside the function.
Your current code is similar to this:

const someFunction = () => {
  const someVariable = "some value"; 
};

console.log(someVariable);

someFunction(); //call the function

Output:

ReferenceError: someVariable is not defined

So, we need to access the variable someVariable from inside the same block where it was defined:

const someFunction = () => {
  const someVariable = "some value";
  console.log(someVariable);
};

someFunction(); //call the function

Output:

some value