Functional Scope | This is a bug, right? (Scope_changing the laundryRoom variable)


#1




Directions are: "...Also, you're lucky enough to have in-unit laundry, so let's re-assign laundryRoom inside our function to: laundryRoom = 'In-unit'."

Will not accept variable within the function without "var" in front, even though it is already declared as a global function. The hint box even shows it without "var" before laundryRoom.

Is "var" really needed inside the function to re-assign it?


_My code:_
var laundryRoom = 'Basement';
var mailRoom = 'Room 1A';

var myApartment = function() {
  var mailBoxNumber = 'Box 3';
  laundryRoom = 'In-unit';
};

_Code it will accept as correct:_ 
var laundryRoom = 'Basement';
var mailRoom = 'Room 1A';

var myApartment = function() {
  var mailBoxNumber = 'Box 3';
  var laundryRoom = 'In-unit';
};


#2

Var is needed because you're establishing a new local only variable. While there is a global variable for laundryRoom, you're just wanting to change the variable inside myApartment. If you didn't add the var, you would change the global variable, and who would want random strangers using your In-unit laundry room?

This is probably a terrible explanation, sorry.
Hope I helped at least a bit :slight_smile:


#3

Thank you! Totally helpful!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.