I NEED HELP pls!

Tell us what’s happening:

it won’t let me pass…I need help. Thanks.

"The editor has two console.log s to help you see what is happening. Check the console as you code to see how it changes. Declare a local variable myVar inside myLocalScope and run the tests.

Note: The console will still have ‘ReferenceError: myVar is not defined’, but this will not cause the tests to fail. "(but i am failing)

Your code so far


function myTest() {
var myVar = "foo";
console.log(myVar);
}
myTest(); // logs "foo"
console.log(myVar); // loc is not defined

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Challenge: Local Scope and Functions

Link to the challenge:

Hello~!

This line correctly declares the variable inside your function. I believe the reason you are not able to pass the tests is that you have changed the code in other places.

I recommend you reset the lesson by clicking “Reset This Lesson”, then “Reset All Code”. Then add the line I’ve quoted above where it says //Only change code below this line.

2 Likes

Thanks! I made it=) Have a good day!

1 Like

The challenge is to understand the Local Scope and Functions. Search on YouTube more about Function Scope. Short story, a variable declared inside a function can’t be seen outside the function. Creating a function creates a special chamber for variables declared inside. You can see other variables from outside the chamber (scope), but you can’t see what is inside from outside unless you return the variable.

If you want this code above to work, you have to redeclare the variable myVar outside the function.

function myTest() {
   var myVar = "foo";
   console.log(myVar);
}

var myVar = "foo";

myTest(); // logs "foo"
console.log(myVar); // now logs 'foo'

Try yourself more games with scope to understand how it works.

Hello~!

I apologise, but this is incorrect. The challenge specifically calls for you to not declare the variable outside of the function - the goal is to demonstrate how function scope works.

Sorry, of course! I didn’t look at the whole challenge. You are right!
Of course, it’s not false what I said, but for this challenge you should not declare a global variable.
My mistake … :pensive: