freeCodeCamp Challenge Guide: Local Scope and Functions

freeCodeCamp Challenge Guide: Local Scope and Functions
0

Local Scope and Functions


Problem Explanation

Local scope means that the variable is available within a certain area. In the case of this exercise, myVar is only available within the function, and not anywhere outside.


Solutions

Solution 1 (Click to Show/Hide)
function myLocalScope() {
  var myVar;
  console.log(myVar);
}
myLocalScope();

Code Explanation

  • The variable only exists in the function. Outside the function, it is non-existent.
5 Likes

Answer is very simple -

function myLocalScope() {
  var myVar = "foo";
  
  console.log(myVar);
}
myLocalScope();
4 Likes

// Declare my global variable here
var myVar = 5;
// or myVar = 5;

function myLocalScope() {
var myVar = “Jo”;

console.log(myVar);
}

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

//how pass the test?

2 Likes

function myLocalScope() {
var myVar = ‘use strict’;
console.log(myVar);
}
myLocalScope();

5 Likes

I’ve been stucked on this challenge for a week and my hardwork paid off and of course with the help of FCC/Help Gitter community. Thanks to @thekholm80

Lessons learned to pass this challenge:

  1. Do not delete/modify anything inside the myLocalScope() function - you will see that there is a 'use strict' line. You need to ignore that. Before I thought that is the string value that we need to assign in the myVar variable, but it’s not.

  2. Assign value to myVar inside the myLocalScope function. You can assign either string or numeric values on it.

  3. Lastly, read the commented out instructions on the console like / / Run and check the console AND // myVar is not defined outside of myLocalScope

The most important of all is // Now remove the console log line to pass the test Ignoring this instruction will not make you pass the challenge.

Hope this hints made you pass the challenge. Remember always Read-Search-Ask

11 Likes

Well, it’s really easy if you follow all the Instructions and Hint
Refreshing the page may help if you get stuck.
Try!
Here is my spoiler (click on the blur :point_down: )

[spoiler]function myLocalScope() {
‘use strict’;

var myVar = 100;
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

[/spoiler]

10 Likes

\This was the most annoying challenge

var myVar = 5;
function myLocalScope() {
‘use strict’;
var myVar = 5;

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

// 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

11 Likes

im just commented this string outside function //console.log(myVar);
because it not correct.

1 Like

I had to check this because i couldn’t understand the problem goal

3 Likes

I’m trying to use console.clear() but don’t work in firefox. maybe could be that?

1 Like

I’m not sure about that but these code should run anyway in FireFox. Can you please refresh your browser?

2 Likes

peep this code right hurrr esp. the part where daniel created the code var myVar and myVar = (making it global)

1 Like