FAQ: Objects - Nested Objects

That produces

{ name: 'Bill' }

Did you submit that as an answer?

The above is the error I reported in my original post. Since this runs without error in VS Code I know that it is not my code. Nevertheless, I cannot move forward.

So this doesn’t pass?

const firstPassenger = spaceship.passengers[0]

Just checked my old work and have a more filled out object:

spaceship.passengers = [
  {name: 'Dave', type: 'human', job: 'pilot'}, 
  {name: 'Frank', type: 'human', job: 'co-pilot'},  
  {name: 'Hal9000', type: 'computer', job: 'systems'}
]

Using your object produced a similar error:

firstPassenger should be assigned to the value at the first index of the spaceship ‘s passenger array.

let spaceship = {
  passengers: 
  [
  {name: 'Dave', type: 'human', job: 'pilot'}, 
  {name: 'Frank', type: 'human', job: 'co-pilot'},  
  {name: 'Hal9000', type: 'computer', job: 'systems'}
]  
  ,
  telescope: {
    yearBuilt: 2018,
    model: "91031-XLT",
    focalLength: 2032 
  },
  crew: {
    captain: { 
      name: 'Sandra', 
      degree: 'Computer Engineering', 
      encourageTeam() { console.log('We got this!') },
     'favorite foods': ['cookies', 'cakes', 'candy', 'spinach'] }
  },
  engine: {
    model: "Nimbus2000"
  },
  nanoelectronics: {
    computer: {
      terabytes: 100,
      monitors: "HD"
    },
    'back-up': {
      battery: "Lithium",
      terabytes: 50
    }
  }
};  
let firstPassenger = spaceship.passengers[0].name // = Dave
let firstPassenger = spaceship.passengers[0] // prints '{ name: 'Dave', type: 'human', job: 'pilot' } '

This is the code that passed previously. I don’t want to reset the exercise in case there is a new bug. Would rather leave the old code alone.

let firstPassenger = spaceship.passengers[0]
print(firstPassenger)

If all else fails, compare your code to the solution and adjust to match.

Why, in this exercise does ‘back-up’ appear in yellow and in quotation marks? Why doesn’t it appear the same as the other key-value pairs i.e. turquoise in colour and no quotation marks?

Without quotes:

back-up: {
        ^
SyntaxError: Unexpected token -

Dash is an invalid character in property name (an identifier). It’s either,

'back-up'

or

backup

or

back_up

let spaceship = {
passengers: null,
telescope: {
yearBuilt: 2018,
model: “91031-XLT”,
focalLength: 2032
},
crew: {
captain: {
name: ‘Sandra’,
degree: ‘Computer Engineering’,
encourageTeam() { console.log(‘We got this!’) },
‘favorite foods’: [‘cookies’, ‘cakes’, ‘candy’, ‘spinach’] }
},
engine: {
model: “Nimbus2000”
},
nanoelectronics: {
computer: {
terabytes: 100,
monitors: “HD”
},
‘back-up’: {
battery: “Lithium”,
terabytes: 50
}
}
};
const capFave = (spaceship.crew.captain[‘favorite foods’])[0];
spaceship.passenger = [
{name: ‘john’},
{name: ‘nick’}
]
const firstPassenger = spaceship.passenger[0];
console.log(firstPassenger); //prints { name: ‘john’ }
/red line under code “Did you change the passengers property on the spaceship object to be an array?”/

Can not move on.

Should that be a plural?

That makes sense. Thanks for clarifying mtf :+1:

1 Like