Mind is disintegrating while trying to wrap around "Accessing Object Properties with Variables"

Tell us what’s happening:
In the explanation, it uses two examples the second one is really difficult for me to understand how it’s doing the same thing as the first example in the challenge…

How does the “Name” portion of propPrefix(“Name”) end up being “John” if “Name” is not defined?

The 2nd example below

Another way you can use this concept is when the property’s name is collected dynamically during the program execution, as follows:

var someObj = {
  propName: "John"
};
function propPrefix(str) {
  var s = "prop";
  return s + str;
}
var someProp = propPrefix("Name"); // someProp now holds the value 'propName'
console.log(someObj[someProp]); // "John"

how is the code output not " propJohn " or “Johnprop” if it says return s + str
and s = "prop" below does s+ str not equal propJohn?

 function propPrefix(str)
   var s = "prop";
   return s + str;

if it says
console.log(someObj[someProp]);
not sure if I didnt pay attention on some of the lessons before but this section is a complete curve ball

my flawed logic leads me to believe

someObj = " John " === John
&
someProp = propPrefix("Name"); === propJohn"Name"

can ‘someProp’ , ‘propName:’, ‘propPrefix’ be changed to any other word || is prop short for property?

If I change the code like below is it still valid? //example below

var dogs = {
  Fido: "Mutt"
};
function dogName(str){
  var s = "Dog"
  return s + str;
}

var someDog = dogName("Name"); //someDog now holds the value ?
console.log(dogs[someDog]);

honestly most of the time FCC shows me 2 ways to do the same thing it’s usually too much to process, another example where this happened was with the counting cards challenge had 2 answers. The syntax of the second one looked like hieroglyphs at first, thankfully going back, and looking at it later it makes more sense. Although at the time of the challenge it just made it more intimidating to learn/continue… because I assumed I wasnt understanding the material as I should

  // Only change code below this line
  var regex = /[JQKA]/;
  if (card > 1 && card < 7) {
    count++;
  } else if (card === 10 || String(card).match(regex)) {
    count--;
  }

  if (count > 0) return count + " Bet";
  return count + " Hold";

  // Only change code above this line
}

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



Challenge: Accessing Object Properties with Variables

Link to the challenge:

someProp will hold the result of the function call to propPrefix passing in the string “Name” to the function. The function propPrefix does just one thing, it adds the string “prop” to the beginning of the string passed into it. So

propPrefix("Name")

returns “propName”. Thus someProp will be set to “propName”.

This can now be rewritten as:

console.log(someObj["propName"]);

And the object someObj does have a property “propName” so the value held in that property (“John” in this case) will be written to the console.

Just take things one line at a time and make sure you understand what each line is doing. Replace the variables with their actual values as you get them.

1 Like

No, it doesn’t, it holds the value “DogName” because the function dogName attaches the string “Dog” to the beginning of whatever string is passed into it (which is “Name” in this case).

1 Like

I think I might be starting to get it, would the use case of something like this be if there are multiple properties in someObj to easily organize them and access them through propPrefix(str)

for example

var someObj = {
  propName: "John",
 propAge: 20,
propHeight: 5

};
function propPrefix(str) {
  var s = "prop";
  return s + str;
}
var someProp = propPrefix("Age"); // someProp now holds the value 'propAge'
console.log(someObj[someProp]); // "20"

would the code above work by changing the string in propPrefix(str)
to any of the values inside of someObj?

Yes, I think you are getting it!

I wouldn’t read too much into this exercise as far as use cases are concerned. This is merely to show you that you can access properties on an object with variables instead of actual strings.

1 Like

Thanks for taking the time out to help

Yes, but you wouldn’t change it to the actual property name in someObj, just the part after “prop”.