Local Scope and Functions error

Tell us what’s happening:

declaring the global variable and a local variable is done but the test won’t pass.
What would be the error?

Your code so far


var myVar = "Hello";

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

// Run and check the console
// myVar is not defined outside of myLocalScope
var myVar = "World";

// 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/74.0.3729.169 Safari/537.36.

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

A function should return something in this case. Does your function return anything?
Does myLocalScope() return anything ? Does it actually return the variable myVar or just has it in the scope?

I believe you are confused by that console.log inside the body of the function.
Try to remove that console.log and console log the output of myLocalScope call (myLocalScope())
Because this is what you want I believe.

You have a myVar variable in the global scope, so you are failing that test that doesn’t want a global myVar

I suggest you reset your code and create myVar only in the function scope as instructed, then run the tests, and then follow instructions in the comments

I just had a look at the challenge. The above comment is the solution in your case. Probably I just end up to confuse you more :)) . Sorry

I read the instruction wrong.
Had to remove all the console.log and return the local variable inside the function without any global variable.
Thank you for your help!

2 Likes