freeCodeCamp Challenge Guide: Global vs Local Scope in Functions

freeCodeCamp Challenge Guide: Global vs Local Scope in Functions
0

#1

It is possible to have both local and global variables with the same name. When you do this, the local variable takes precedence over the global variable.

In this example:

var someVar = "Hat";
function myOutfit() {
  var someVar = "Head";
  return someVar;
}

The function myOutfit will return "Head" because the local version of the variable is present.

// Setup
var outerWear = “T-Shirt”;

function myOutfit() {
// Only change code below this line
var outerWear = “sweater”;

// Only change code above this line
return outerWear;
}

//myOutfit();


Global Vs Local Scope in Functions Help
#2

The solution to this problem is:

// Setup
var outerWear = “T-Shirt”;

function myOutfit() {
// Only change code below this line
var outerWear = “sweater”;

// Only change code above this line
return outerWear;
}

myOutfit();


#3

But why put a ready-made solution here? The idea is to learn something))
I would say “use var” before the variable . Or something like that


#4

To pass the challenge, you need to create a local variable inside **myOutfit** function that will return a string value of “sweater”.


#5

my understanding was that you will define myOutfit and assign a new variable to to.
woule be something like this,
// Setup
var outerWear = “T-Shirt”;

function myOutfit() {
// Only change code below this line

var myOutfit =“sweater”;

// Only change code above this line
return outerWear;
}

myOutfit();

but the correct solution is

// Setup
var outerWear = “T-Shirt”;

function myOutfit() {
// Only change code below this line

var outerWear =“sweater”;

// Only change code above this line
return outerWear;
}

myOutfit();