Looping through Objects Help

Could anyone please help me with this piece of code, I’m stuck on the 1st one

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

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)
};

Please anyone answer asap
Thanks

There are quite a lot of different problems with the code you have posted.

I don’t know if the formatting of your code has been messed up when you copied here but hopefully it looks a big clearer in the codecademy window for you. When you copy code here you can use the code tags to make it easier to read which will increase the chance that someone will take the time to help.

With a little re-formatting your code looks like this:

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 (spaceship.crew.innerObject) {
    console.log(${crew memeber's role]: [crew member's name}.propertyName)
};

Your question was about looping so I’ll start there. There are several for loops in JS but the one you need to iterate over an object’s keys like that is the for, in loop. The syntax is like so:

for (let key in spaceship.crew){
   //use the key to access the object
}

Remember you can reference a keys value like so: spaceship.crew[key].

When you use string interpolation, as you do in the for loops console.log() statement you need to enclose the entire string in backticks. That is the character to the left of the 1 key on most keyboard I’ve used. An example of that would be:

let numberOne = 1;
console.log(`You're number ${numberOne}`);

You only enclose the variable that you are inserting into the string with ${} such as ${variable}.

In your object declaration when adding a function to an object, the syntax follows the same pattern as declaring any other variable to a key. That is: key: value. A function in JS can be used and passed around just like any other variable.

EG.

const saySomething = "Hello, World";
const myObject = {
   doSomething: function(somethingTodo) {
      console.log(somethingTodo);
   }
}

myObject.doSomething(saySomething);

Currently the functions in your object are not declared properly and even if you get the loop to work, the functions won’t work.

In addition, I don’t know if it is a formatting problem, but some of the quotation marks you have in your code are not valid characters for use in JS code. Make sure you’re using normal single quotes and not “curly” ones - your code won’t work with them.

I would recommend that you go back and revisit some of the lessons on Codecademy around objects and loops. Then try and tackle the spaceship question again once you have refreshed your memory of those fundamentals.

1 Like