Acces Objects with variables

Hi World,

I´ve just passed the challenge called ** Accessing Object Properties with Variables** using this code:

// Setup
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};

// Only change code below this line

var playerNumber = "Montana";

var playerNumber = 16;       // Change this line
var player = testObj[playerNumber];   // Change this line

How it is possible if I´ve declare two times and with different values the variable playerName?. It worked only with:

var player = testObj[16];

but I must use [playerNumber] variable.

Could you help me to understand this point?

Thanks.

The second time you are replacing the value of playerNumber from “Montana” to 16. It’s incorrect to use the var keyword twice, but JavaScript can be pretty permissive and will sometimes let you do things that are not a good idea.

1 Like

ArielLeslie,

thank you for your answer. I ´ll pay attention to that!

Appreciated.