Help with Accessing Object Properties with Variables

trying to do this exercise:
Use the playerNumber variable to look up player 16 in testObj using bracket notation. Then assign that name to the player variable.

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

// Only change code below this line

var playerNumber =;       // Change this line
var player = testObj;   // Change this line

not to sure about this one, i thought it might be:

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

// Only change code below this line

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

Update:
so I have got this far:

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

// Only change code below this line

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

Please link to the challenge when asking for help (challenge).

You have to use the playerNumber variable for the lookup, not the string "Montana".

I have also tried removing the quotations as well.

“Montana” is the value of testObj.16, not a variable.
You have a variable that holds a property name of testObj. You need to use that variable to access a value in in testObj.

So does ‘player’ hold the property name?
many thanks

player is what you are assigning a value to. It doesn’t exist before the line where it is declared.

This is so difficult!!!
let me see, if I can work it out though, so I learn :+1:

Think of this frustration as the mental equivalent of sweat and sore muscles. :muscle: :brain:

According to the example:

var dogs = {
  Fido: "Mutt",  Hunter: "Doberman",  Snoopie: "Beagle"
};
var myDog = "Hunter";
var myBreed = dogs[myDog];
console.log(myBreed); // "Doberman"


I see that we assing the value of 'Hunter ’ to the variable of myDog, so in the exercise we are assigning the value of ‘16’ to variable playerNumber…
then we use the bracket notation to assign ‘playerNumber’ to player.

this is how I understand it

As said, player should have the value from the lookup.

You can think of an object property like an index into an array. Referring to the property on the object will give the value.

const object = {
  property: "value"
}

object["property"] === "value"

const array [1,2,3]

array[0] === 1

Look at the example code given in the challenge:

var dogs = {
  Fido: 'Mutt',
  Hunter: 'Doberman',
  Snoopie: 'Beagle'
};
var myDog = 'Hunter'; // myDog is the string 'Hunter'
var myBreed = dogs[myDog]; // same as doing dogs['Hunter'] just using a variable
console.log(myBreed); // "Doberman"

almost there
i know have the following:

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

It sounds like you’ve got it. What’s your code? (Sometimes it is best to reset the code if you’ve been trying a lot of different things, because other changes can sneak their way into the boilerplate.)

Not quite:
here is my code:

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

// Only change code below this line

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

and I have tried it without quotations as well.

the message I get is:
You should be using the variable

playerNumber

in your bracket notation

var playerNumber = 16;       // Change this line

You’re not using this variable at all.

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

This code is not using a variable to access an object property. It is using a string.

:dizzy_face: :dizzy_face: :persevere: :persevere:

So if I do this:

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

It still give me the same error…
16 is the value of ‘playerNumber’ so I understood that that was what needed to go into the bracket notation.

but you are not using the variable playerNumber.
If I change the value of playerNumber, the value of player stay the same.

You’re still not doing anything with playerNumber.
You’re still not using a variable in your second line.

it says that : The variable player should be a string
I thought strings were in quotations.??