freeCodeCamp Challenge Guide: Local Scope and Functions

freeCodeCamp Challenge Guide: Local Scope and Functions
0

#1

Variables which are declared within a function, as well as the function parameters have local scope. That means, they are only visible within that function.

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

function myLocalScope() {
  var loc = "foo";
  console.log(loc);
}
myLocalScope(); // "foo"
console.log(loc); // "undefined"

loc is not defined outside of the function.


#2

Answer is very simple -

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

#3

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


#4

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


#5

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


#6

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]


#7

\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


#8

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


#9

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


#10

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


#11

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


#12

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