Https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/for-in

why this code doent work? What’s wrong with it:

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!') } 

    }

}

};

for(let crewMemberName in spaceship.crew.name){

console.log(${crewMemberName}: ${spaceship.crew[crewMemberName].degree});

}

name is not a property of crew.

for {crewmember in spaceship.crew) {}

Now crewmember is one of the contained properties, ‘captain’, ‘chief officer’, ‘medic’, ‘translator’, each of which have a name property.

Owing that crewmember is a variable we cannot use dot notation. Instead we need to use subscript (bracket) notation.

spaceship.crew[crewmember].name
1 Like

Thanks! I guess I need to be a little more cautious!

1 Like