Local Scope and Functions (refresh not working)

Local Scope and Functions (refresh not working)
0

#1

Tell us what’s happening:
So I figured out this first checkmark (Add a local myVar variable) but I can’t figure out how to do “No global myVar variable” I’ve looked through and there a few forums like this but the answers weren’t clear. Basically, they say after you have the first checkmark, refresh, delete “console.log(myVar);” and you should have it. I have tried it with both console logs but mainly the first one. Should I be doing it with the second one or is the first one right? Am I refreshing out of order or something? Anyway, thanks for reading. All your clarification is appreciated!

Your code so far


function myLocalScope() {
  'use strict'; // you shouldn't need to edit this line
  

console.log(myVar);
}
myLocalScope();
var myVar = "strict";
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions/


#2

You declared a local variable, but according to the instructions, you are supposed to declare the variable inside the function named myLocalScope.


#3

Yea I thought that but when I make it:

function myLocalScope(myVar = “strict”) {
‘use strict’; // you shouldn’t need to edit this line

console.log(myVar);
}
myLocalScope();

It doesnt work.


#4

You still have not declared the variable myVar inside the myLocalScope function. You have created a default value for myVar, but that is not the same thing, because you are using strict mode (the ‘use strict’ part of the code). Just declare myVar and assign it a value as you would normally.


#5

I keep looking back at the example:

Here is a function myTest with a local variable called loc.

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

So wouldnt this be the same as me saying:

function myLocalScope() {
‘use strict’; // you shouldn’t need to edit this line
var myVar = “strict”;
console.log(myVar);
}
myLocalScope();

I dont understand what the difference is other than labels.


#6

I am confused. Which code are you currently running the tests on? Also, what errors show in the bottom right section?

The first code will not work, because the instructions tell you to use a variable named myVar and not loc.

Also, do not try and bold your code, because it puts extra ** and ** in the code you post. Simply paste all the code that shows in the editor section and put 3 backticks on the line before the code you paste and 3 backticks on the line after the code you paste.


#7

the first code was not mine, it was the example of what a local variable should look like. I was wondering what the difference was between that and what mine says. Based off that I should be getting the “Add a local myVar variable” check mark. I am defiantly the reason you are confused, I’ve just been stuck on this problem for almost an hour now and even when I try to directly copy someone else solution I can’t complete it and i’m starting to get frustrated.


#8
function myLocalScope() {
  'use strict'; // you shouldn't need to edit this line
  var myVar = "strict";
  console.log(myVar);
}
myLocalScope();

When you enter the above, you will still need to following the rest of the challenge instructions as you will not be passing either test. You will also see the following in the bottom right section:

// running test
myVar is not defined
myVar is not defined
// tests completed

Once you do what the remaining instruction requests, myVar will no longer be undefined in the global scope.


#9

So after experimenting and going through other tests about functions i finally got the solution. What i did was have:
var myVar = “strict”;
function myLocalScope() {
‘use strict’; // you shouldn’t need to edit this line
var myVar = “strict”;
console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log(myVar);

// Now remove the console log line to pass the test

Then I refreshed and deleted:
var myVar = “strict”; (one nearest to the top of the screen)
and both console.log(myVar);

I was way overcomplicationg things. I wast trying to add and change way to many elements and was’nt pinpointing the main question. Im still not 100% sure on why I needed to refresh and remove lines of code however. Anyway, you really did help and I am reliefed to move on!