Two questions re: the following code:
var answer
function sequentialSizes(val) {
answer= "";
switch (val) {
case 1:
case 2:
case 3:
answer = "Low";
break;
case 4:
case 5:
case 6:
answer = "Mid";
break;
case 7:
case 8:
case 9:
answer = "High";
break;
}
// Only change code above this line
return answer;
}
sequentialSizes(1);
console.log(answer) //prints Low
var outerWear
function myOutfit() {
outerWear = "sweater"
return outerWear;
}
myOutfit();
console.log(outerWear) //prints sweater
First, in either example, if I don’t call the function by removing the lines
sequentialSizes(1)
or
myOutfit()
undefined is printed to console for the respective variable, answer or outerWear.
Why is that? I would have thought that the variables were declared globally within the functions in these examples, in the absence of var, such that the function wouldn’t have to be called subsequently for the variables to be re-assigned from within the function?
Second question: if I re-write any of the lines within the switch statements from the first example from
answer ="Low" // or "Mid" or "High"
to
var answer="Low" // or "Mid" or "High"
the variable answer reads undefined in the console- it seems the scope of answer within the switch statement is only global if it is global in every instance? but if it is assigned with var in any instance, it is local for the entire switch statement? Is that right? Is that an aspect of switch statements specifically?
Thanks for any insight!
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0
.
Challenge: Multiple Identical Options in Switch Statements
Link to the challenge: