Why can't I use crewMember.name?

So basically in my own words the variable crewMember is not even pointing at the object or the object element. The loop literally iterated over the objects crew and grabbed the member and converted it to a string and put it in the variable. The variable is just that a variable with a string that was grabbed from an object but doesn’t point to the object. That is why we have to point to the object all over again by using spaceship.crew[crewMember].name. ← this code is the same as writing spaceship.crew[‘captain’].name only it is using the string already in the variable rather than the user having to write it out.

Also, this is the end of the Objects Lesson, is there anywhere that I can practice what I learned here. Like something that has challenges tailored to this beginning part of objects. I feel like I grasped it but I need to practice it a lot and I want to practice before moving on to advanced objects.

This is the format used to concatenate strings with backticks.

1 Like

This helps a bunch!!! The explanation is linear and easy! Thank you!

I knew that this declaration format is using the ‘accessing’ pattern to enter one object at a time, but that square bracket messed me up and couldn’t find another piece of logic, until you explained it to me!

Thank you so much! :slight_smile:

Yes, also called String Interpolation :wink:

This reply helped in clarifying alot. Thank you very much.

Is there ever a case where you don’t know how the key value will be formatted?

In this case, we know that one of the crewMember keys is formatted as a string with a space. That requires [crewMember] instead of .crewMember.

However, what if we didn’t know how the object would be formatted? Or, if it was so large that it wasn’t feasible to check every result? Or is this simply not the case? Is there some sort of logic you would add in to check the type of each first?

Hopefully this makes sense. Thanks in advance!

the dot notation can only be used if the developer uses the property name, so if we have a variable (with a loop, parameter, or whatever other cause) you have to use square brackets (associative array notation)

1 Like