Accessing object with variable

guys , my question is that in the code var playerNumber = 16 ;
16 is an example in this challenge? I mean it might be other numbers as well in stead of 16. can we write a general code for any other number? Thanks.

Your code so 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[playerNumber];   // Change this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5.

Challenge: Accessing Object Properties with Variables

Link to the challenge:

what would work or not, depends on the object you have, in this case it would work with 16, 12 and 19 as those are the property names of the object

1 Like

Yes, you can write general code for any other number as long as it is defined as the object key. Like in this case it is done for 12, 16 and 19.

1 Like

@narmada_nannaka @ilenia w hat is not clear for is that why we have to assign the code to 16 ? as I tried with console.log if I change the number it gives the name of player. but why wrote var playerNumber = 16 ; whereas, we have 3 different properties,

@narmada_nannaka @ilenia

it is showing how to get one property value and also how to access a property value using a variable

sometimes to show how a thing works, meaningless things are used

1 Like

aha okkk, thanksssss