How can i do this please can someone help?

Tell us what’s happening:

Your code so far


// Setup
var outerWear= "sweater";

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

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

myOutfit();

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Global vs. Local Scope in Functions

Link to the challenge:

You should not change the value of the global outerWear . How???

You don’t want to change the global variable. You need to reset your code and only make your changes where the comments tell you to.

This exercise is trying to explain to you how global scope works.
You are initialising the outWear var twice. Once outside of the function and once inside.

how does the inside one work? is it var innerWear.

The inside declaration works as you’ve done it. You’ve just modified code you weren’t supposed to which is failing the tests.

iv taken out 1 of the outerWears, which gives me the correct answer. just not understanding the global scope

What is your current code?

What part of the challenge description can I clarify? If you declare the same variable both as a global variable and a local variable inside a function, the local definition overrides the global value inside of the function.

// Setup
var outerWear = “sweater”;

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

}

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

myOutfit();

You changed code above the line that says “Only change code above this line”. You don’t want to do this!

You need to reset your code. You should only add the definition of the variable outerWear to the function.

the code is like this above the part is states '“only change code above this line”.

i shall delete that in that case

// Setup
var outerWear = "T-Shirt";  // <- DO NOT CHANGE THIS

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

// THIS IS THE ONLY PLACE WERE YOU SHOULD MAKE CHANGES

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

myOutfit();

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

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

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

console.log(myOutfit());
console.log(outerWear);

Closer. Your code won’t run since you have a syntax bug. You have an extra }.

Also won’t pass the test because "T-shirt" is changed from the original "T-Shirt"

1 Like

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

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

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

console.log(myOutfit());

I added a couple of comments. You are close.