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!

21 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:

2 Likes

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:

Agreed that this is the best explanation in the thread.

It seems like ${number} in your code only works because it’s the highest level of the hierarchy in the outer.inner object. Seems like if you go any deeper than that, you have to re-state outer.inner[number]. and then continue to specify more granularity. Seems unnecessary to me because I would expect it to be implied that we are always referencing something within the outer.inner object since it’s specified in the for/in loop. Seems like console.log([number].name) should imply that it’s outer.inner[number].name but apparently it just doesn’t work that way.

1 Like

Solution:

Step 1.

for (let crewMember in spaceship.crew) {

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

Step: 2

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

Output:

captain: Lily
chief officer: Dan
medic: Clementine
translator: Shauna
Lily: Computer Engineering
Dan: Aerospace Engineering
Clementine: Physics
Shauna: Conservation Science

Hi,
As per MDN documentation what I have understood. A for...in loop only iterates over enumerable, non-Symbol properties. So in this case it will not iterates over method().

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

what exactly does the ‘:’ colon operator do in this instance? and can someone break this syntax in this lesson down for me, in particular the syntax used to produce the answer to question 2?

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

im slowly getting a grasp of the concepts needed to confidently write JS but every now and then something comes along which completely throws me.
thanks in advance
rob

Nothing special. Just part of the string you print. Just a nice way to list name: degree.

thanks for getting back to me so promptly, the level of support this course offers, even for us peasants taking the free version is second to none.

okay, so would you mind breaking down the syntax used to answer the second question for me? i think im getting confused accessing the nested properties.
thanks again in advance.

Have you attempted to break down the code yourself? There also tools available like this one:

http://www.pythontutor.com/visualize.html#mode=edit

to help you step through your code

Why do you struggle? So I know where i can start my break down. What do you grasp, and where do you get lost?

2 Likes

i didnt know such a website existed. thankyou i will take the time to execute the code in this. i always prefer to be able to work it out myself as the solution or concept will stick better. stand by for more questions :smiley:

1 Like

this code visualiser has helped but it has raised other questions. firstly, as seen in the image, it defines the frame, but then gives two block fields (position). 1) im guessing block one is: ${spaceship.crew[position].name}
and block 2 is:

${spaceship.crew[position].degree}

so why does block 1 always remain undefined throughout the whole process? or am i interpreting the visualiser wrongly?
I don’t want to advance myself through the lessons untill i understand this.

Thanks again in advance.

image

No idea, I just extract the information I needed.

1 Like