Global vs Local Scope in Functions

Global vs Local Scope in Functions
0

#1

My code given below and it is right too and showing error…

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


#2

Note on the capitalization :wink: . "Sweater" should be "sweater".


#3

@shibuvarughese1971 You need to write sweater in small letters. You have used capital S for the value.


#4

I can’t really tell what you’re trying to do, but it looks like you shouldn’t have the ‘var’ before the outerWear = “Sweater” line. This means you’re creating a new variable, rather than editing the variable declared in the first line. i.e. The outerWear variable inside the myOutfit function is a different variable, so changing it won’t affect the one on the first line.

Try this instead:

var outerWear = "T-Shirt";

function changeMyOutfit(){
    outerWear = "Sweater";
}

changeMyOutfit();

Notice the ‘var’ is no longer there. Also, I changed the name of the function to make it more clear what the function does. This is very helpful for the future, believe me! :slight_smile: