Looping Through Objects

The thing I’m supposed to do is the following:

Using for...in , iterate through the spaceship.crew object in the code editor and console.log() a list of crew roles and names in the following format: '[crew member's role]: [crew member's name]' , e.g., 'chief officer: Dan'

However I have no idea waht to do, I read the for in document on mozilla but I still have no clue what any of it means. The hint also gives very little value since I pretty much only know what half of the symbols there does.

i.e this is the hint that is provided in the first task.

for (let variableName in outerObject.innerObject) {
  console.log(`${variableName}: ${outerObject.innerObject[variableName].propertyName}`)

However I have no clue what this means, what does the dollar sign do why is it written in that order etc. Please help me with this because I’m feeling completely lost right now…

console.log(`${variableName}: ${outerObject.innerObject[variableName].propertyName}`)

You should probably be using for of, not for in, for in includes inherited properties… unless you specifically want that
(also codecademy has a tendency to treat your code like TEXT when inpsecting it (as opposed to testing behaviour) and therefore expect in, not of, even though that’s more reasonable)

template strings, since you’re already familiar with mdn, go have a look at it there

also, maybe ignore the hint and consider what behaviour needs to exist and go on from there… which is probably a good idea in general when it comes to “hints” especially since a lot of so called hints are just full solutions

1 Like

Ok, I’ve not done anything with for of yet and the exercise specifically asks of me to do this with for in, so I will probably go with that for now but will definitely check what for of does later!

Thanks for the help!

If you can identify what’s blocking you then that’d be something to poke at, a subproblem to solve before continuing with the bigger problem.
And if you can solve parts of the problem then that might tell you something about what the remaining gaps need to be filled with.

Yes, I think what’s blocking me is the use of the backticks since it goes against my intuition of using citation marks around strings and then the + operator to add the string.

But I will keep googling some more about the template string and looping through objects to get a better understanding of it. Thanks for the help!

You could do that.
And for short strings that’s totally fine.

For long strings you wouldn’t want to make lots of copies (+) and instead concatenate them all at once.

Might be a bit cumbersome to open and close strings and sprinkle plus all over the place… But historically speaking we’ve probably done this … a lot.

String interpolation gets rid of a bunch of cruft (so we get to look at the parts we care about)… I guess their purpose is to be neat, and not too much else.
Looking at mdn though, tagged templates, especially when producing non-strings, could probably used for some fun things…

Yeah, you’re probably right, will try to use string interpolation more in the future since it’s probably the better option.

I appreciate your help! :smiley:

This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.