Why can't I use crewMember.name?

Thanks for the clarification!

Why can’t we use (${spaceship.crew[crewMember]}) in the first for loop instead of only using (${crewMember})? Aren’t they supposed to be indicating the same thing?

If not why can’t we just use ${[crewMember].name} in the second loop instead of ${spaceship.crew[crewMember].name}?

for (let crewMember in spaceship.crew) {
  console.log(`${crewMember}: ${spaceship.crew[crewMember].name}`)
};

for (let crewMember in spaceship.crew) {
  console.log(`${spaceship.crew[crewMember].name}: ${spaceship.crew[crewMember].degree}`)
};

2 Likes

Can someone tell me why this option doesn’t work? Am I missing something?

// Write your code below
for (let crewMember in spaceship.crew) {
console.log(${crewMember}: ${spaceship.crew[crewMember].name})
};

PS I included backticks in the console statement.

A simple example with some notes I made for myself. Maybe this will help wrap your head around this. Please comment if there are mistakes or it’s wrong somehow!

outer = {
  inner: {
    one: {
      name: "One"
    },  
    two: {
      name: "Two"
    },
    three: {
      name: "Three"
    }
  }
}
for (let number in outer.inner) {
  console.log(`${number}`);
};

When iterating through the object, the for … in loop saves the variable number’s value as a string (with single quotations as they are), so in the first iteration the number variable would contain the value ‘one’ NOT the value outer.inner[‘one’] (as you might think, and it would need to be for it to work without the whole path).

Again, the for…in iterator variable contains simply the property names (which could be anything!) of the object it’s pointing to (outer.inner in this case) as a string. Does not include the leading path that points to the object and isn’t a reference to that property (which does feel kind of wrong somehow and caused the confusion for me at least).

for (let number in outer.inner) {
  console.log(`${number}`);
};

In the first iteration the action performed would be:

console.log('one');

This works fine!

But if we want something inside of the objects, we might do

for (let number in outer.inner) {
  console.log([number].name);
};

But hey, that doesn’t work. That’s because in the first iteration the action performed would be

console.log(['one'].name);

JS would say “I can’t find an object ‘one’ with name property. Where is it?” JS does not know the path to it as the variable only contained a string.

If we add the path to the object

console.log(outer.inner[number].name)

In the first iteration this would be

console.log(outer.inner['one'].name)

Note! You have to use the brackets because the value inside the variable is, you know it, a string with single quotations, as it should.

Now JS is saying: Ah okay, the ‘one’ property in the inner object that is inside the outer object!

7 Likes

How can we access the third iteration, the one with the cheerTeam, announce…etc ?

let spaceship = {

crew: {

captain: { 

    name: 'Lily', 

    degree: 'Computer Engineering', 

    cheerTeam() { console.log('You got this!') } 

    },

'chief officer': { 

    name: 'Dan', 

    degree: 'Aerospace Engineering', 

    agree() { console.log('I agree, captain!') } 

    },

medic: { 

    name: 'Clementine', 

    degree: 'Physics', 

    announce() { console.log(`Jets on!`) } },

translator: {

    name: 'Shauna', 

    degree: 'Conservation Science', 

    powerFuel() { console.log('The tank is full!') } 

    }

}

};

Jeeeeeeeesssuuss

64 entries for someone to just explain it like a normal human being. Although I think you might be breaking the rules which state you can only answer questions on the forum with riddles or conundrums !

Thank you :+1:

1 Like

Great explanation, thank you!

Seriously this is the clearest answer here after so long. Thank you!

such a great explanation, thank you !
if i may suggest it would be more intuitive if you set letter as name of variables. i hope i don’t sound presumptuous.

your way of anwering is so great and completely clear thank you so much :grinning: :grinning: