cjxsam
December 14, 2023, 10:07am
1
Hello, could anyone please help me with this lesson, I am stuck on question 1
Please anyone answer asap.
Code: 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!’) }
}
}
};
// Write your code below
for (spaceship.crew.innerObject) {
console.log(${crew memeber's role]: [crew member's name}.propertyName
)
};
`
Link: https://www.codecademy.com/courses/introduction-to-javascript/lessons/objects/exercises/for-in
code given
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!') }
}
}
};
I think that you would use the for-in stuff with a temporary variable instead of using .innerObject
like this:
for (let member in spaceship.crew) {
and then for that member
on each iteration of the loop, you’d use the member
for string that stores the property’s name (which is the role here),
and spaceship.crew[member]
to access the object [that represents that member of the crew].
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 member in spaceship.crew) {
console.log(member);
console.log(spaceship.crew[member]["name"]);
console.log(' \n');
}