Accessing Object Properties with Variables

Accessing Object Properties with Variables
0.0 0

#1

Tell us what’s happening:
I need some help I cannot find out how to do this I’ve been stuck on it for an hour I’d apreciate the help.

Your code so far


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

// Only change code below this line;

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/accessing-object-properties-with-variables


#2

With objects, you can access them using variables:

anObject[someVariable]

what you need to do is assign “Monatana” to the player variable:

var player = testObj[16]

In order to complete the challenge, however, you need to pass a variable instead of the number 16.

 var player = testObj[playerNumber]

All there is left to work out is what you need to pass to playerNumber:

var playerNumber = ????

Hope this helps :slight_smile:


#3

Well, it did but the playernumber is still a problem but thanks for trying :Dᅠᅠᅠᅠ


#4

Can you show us your latest attempt at a solution? To enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#5
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};

// Only change code below this line;
var playerNumber = testObj[16];       // Change this Line
var player = testObj[16];   // Change this Line

console.log(player);```

#6

What value does playerNumber have after the above line executes? It should have the value 16. What is the easiest way to assign the value 16 to playerNumber?

The instructions say “Use the playerNumber variable to look up player 16 in testObj using bracket notation. Then assign that name to the player variable.” Your code is not using playerNumber to access the property of testObj. Your code does access the value of testObj’s property nameed “16” and assign it to player, but you are supposed to be using the playerNumber variable to access the value and assign it to player.


#8

It is saying assign a number to playerNumber.


#9

So do I assign 16 to playerNumber?


#10

Yes, and then you need to pass playerNumber into the brackets


#11

FINALLY! I got it thank you

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

// Only change code below this line;
var playerNumber = 16;       // Change this Line
var player = testObj[playerNumber];   // Change this Line

console.log(player);```

#12

No. If you look at the challenge example (see below), I have added comments to explain what is going on, so you can better understand what to do.

var dogs = {
  Fido: "Mutt", Hunter: "Doberman", Snoopie: "Beagle"
};
var myDog = "Hunter"; // assigns a value to the myDog variable
var myBreed = dogs[myDog];  // accesses the "Doberman" value of "Hunter" property using the myDog variable and assigns it to the variable myBreed
console.log(myBreed); // "Doberman"