Team Stats Project - Unexpected token ':'

Hey folks, hope you’re doing fine!

I’m stuck at the beggining of the Team Stats Project.

For some reason the program cannot read my code, and it returns this message: “SyntaxError: Unexpected token ‘:’”
It seems there’s something wrong in line 3, but I don’t see anything wrong.

What am I doing wrong?

My code so far:

const team = {
  _players: [
    'MichaelJordan': {
      firstName: 'Michael',
      lastName: 'Jordan',
      age: 30
    },
    'Serena Williams': {
      firstName: 'Serena',
      lastName: 'Williams',
      age: 40
    },
    'Louis Hamilton': {
      firstName: 'Louis',
      lastName: 'Hamilton',
      age: 25
    }
  ],
  _games: [
    game1: {
      opponent: 'John',
      teamPoints: 2,
      opponentPoints: 1
    },
    game2: {
      opponent: 'Belle',
      teamPoints: 2,
      opponentPoints: 1
    },
    game3: {
      opponent: 'PL',
      teamPoints: 2,
      opponentPoints: 0
    }
  ]
  get players() {
    return this._players
  }
}
team.players();

I can’t view the project, so I don’t know the instructions mentioned in the steps.

But, I am guessing that _players and _games are meant to be arrays of objects i.e. they are expected to have a structure like:

[ {}, {}, ... , {} ]

whereas you are trying to use the structure:

[ "abc": {}, "def": {}, .... , "xyz": {} ]

With the above in mind, consider using the structure:

const team = {
  _players: [
    {
      firstName: 'Michael',
      lastName: 'Jordan',
      age: 30
    },
    {
      firstName: 'Serena',
      lastName: 'Williams',
      age: 40
    },
    {
      firstName: 'Louis',
      lastName: 'Hamilton',
      age: 25
    }
  ], 
  ...

Other than that, you are also missing a comma before get players() i.e.

// You wrote:
 game3: {
      opponent: 'PL',
      teamPoints: 2,
      opponentPoints: 0
    }
  ]
  get players() {
    return this._players
  }

// It should be:
 {
      opponent: 'PL',
      teamPoints: 2,
      opponentPoints: 0
    }
  ],       // You are missing this comma
  get players() {
    return this._players
  }

Also, since you are using the get keyword, so the proper syntax for calling the getter is team.players instead of team.players(). If the get keyword wasn’t used, then team.players() would be a valid method call.
So, to view the players you can use:

console.log(team.players);
2 Likes

Hey @ribeka !

I think the issue is that your _players is a mix between an array and an object. I would recommend implementing @mtrtmk 's solution.

Keep in mind that an object is a key-value pair, and an array is a special case of an object. An array has built-in keys, which are called indices or an index. So you can think of your syntax as adding a key to a value. Instead you need to add an object as a value.

We also don’t know your end goal. So although you can create an array of objects, you can also create an object of all the players. It really depends on what you plan to do.

_players: {
    'MichaelJordan': {
      firstName: 'Michael',
      lastName: 'Jordan',
      age: 30
    },
    'Serena Williams': {
      firstName: 'Serena',
      lastName: 'Williams',
      age: 40
    },
    'Louis Hamilton': {
      firstName: 'Louis',
      lastName: 'Hamilton',
      age: 25
    }
  }
2 Likes

Thank you so so much! Now it’s super clear! :heart_eyes: :pray:

2 Likes

Thank you so so much! I tried the way you two told me to and it totally worked! :heart_eyes: :pray:

2 Likes