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

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

#5

You need to post your current code for us to see what you tried.


#6

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


#7

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


#8

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


#9
// 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.


#10

The instructions could be reworded as the following. See if this helps you solve the challenge.

Declare a local variable inside the myOutfit function and name it outerWear. Then, assign it the string value of “sweater”. When you assign “sweater” to the local variable outerWear, it will allow the myOutfit function to return “sweater” instead of the global variable’s value of “T-Shirt” with the added benefit of not changing the global variable’s value at all.


#11

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


#12

See my last reply for additional help and guidance.


#13

You did this inside the function?


#14

yes it comes back as Outfit should return as sweater


#15

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


#16

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


#17

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…


#18

u can try! it’s working


#19

Can u explain me? why it’s working?


#20

if my solution is wrong…


#21

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.


#22

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


#23

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…)


#24

if u click on a hint u will get error 404 page not found