Global vs. Local Scope in Functions "I tried" but need Help[

Tell us what’s happening:

This is what I got so far…

Your code so far


// 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();

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54.

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

You don’t need to declare a variable called myOutfit. The instruction just says add a local variable to myOutfit function. Follow the given example, re-delcare outerWear variable with value of sweater

1 Like

the wording is a bit confusing. You need to override outerWear so change

var myOutfit

to

var outerWear

2 Likes

I tried it but it don’t work

\same as above but it changed it to Outfit = “sweater” it says Outfit is not a function

That is not what we told you to do. you need to use the outerWear variable.

1 Like

I removed the t shirt and placed sweater at the top it comes back right but it says not to change the value of global outerware

// 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();

Do not change the code at the top.

Replace var myOutfit with var outerWear

You must set the variable twice.

1 Like

I have don:
var myOutfit = “sweater”
var outerWare = " sweater"
myOutfit = “sweater”
outerWare = “sweater”
changer the t shirt
changed the last myOutfit & tried adding sweater to it which it said not to do
sweater by itself

You did this inside the function?

1 Like

yes it comes back as Outfit should return as sweater

I GOT IT “MISSPELLING” SORRY I NEED TO GO TO SLEEP
Thank you all for the help…

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

myOutfit();

this is your solution! U have to write second variable in return if u need to return two or more variables

Are you sure? If you edit the code outside of the allowed part the tests are not going to work and you are not able to pass the challenge. Still, this is not what the challenge asks for…

u can try! it’s working

Can u explain me? why it’s working?

if my solution is wrong…

Don’t make an habit of changing code outside of the allowed part, more often than not it will not work.

You are not doing what the challenge asks from you which is creating a local variable of the same name of a global variable, to understand how global vs local scope works.

// 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();

it’s working to. Global variable is still “T-Shirt” and locally variable is now new

That is the right answer.

He other one works because the tests can’t predict everything you will do and you can find “solutions” that will let you pass the tests anyway


If you want to help, it is better to give hints and pointing errors instead of the answer so that other people can also learn how to solve problems. It is difficult to learn something new if you have a solution to copy and paste (and if someone wants that they can go to the Hint section…)