Global vs. Local Scope in Functions - ra

I don’t know ho to solve this, and they error mi “function myOutfit doesn’t exist”.

Your code so far


// Setup
var outerWear = "T-Shirt";

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

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

so within your function, you create a local variable of the same name, then try to call the function within itself. So when JS looks for that name it starts locally, finds the string, and can’t run a string as a function and fails. In this case, thats a GOOD thing -if you comment out the variable, you would have a function that calls itself in an infinite loop.

1 Like

the point of the lesson is to create a vaiable in your function called outerWear, thats all. Then, when you return outerWear, which is returned - the one IN your funcion, or the one OUTSIDE your function?

1 Like

thank you, man :smiley:

correct code, answer

// Setup
var outerWear = "T-Shirt";

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

Almost. :wink:

What you’re doing is replacing the value of the global outerWear. After your function, the original outerWear value is gone. Instead, inside your function, make that line

var outerWear = myOutfit;

This creates a new local variable of the same name, and leaves the other alone.

1 Like