Team Stats .... .push()

Hi! I am working on the Team Stats (https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-objects/projects/team-stats)

On line 51 and 60, the .push() method is giving me problems.

What am I doing incorrectly?

const team = {
  _players: [
    {
      firstName: 'Dan',
      lastName: 'Boyd',
      age: 48
    },
    {
      firstName: 'Rob',
      lastName: 'Sanchez',
      age: 45
    },
    {
      firstName: 'Toby',
      lastName: 'Largust',
      age: 41
    }
  ],
  _games: [
    {
      opponent: 'Broncos',
      teamPoints: 42,
      opponentPoints: 27
    },
    {
      opponent: 'Lightnings',
      teamPoints: 40,
      opponentPoints: 18
    },
    {
      opponent: 'Ravens',
      teamPoints: 82,
      opponentPoints: 40
    }
  ],

  get _players() {
    return this._players;
  },
  get _games() {
    return this._games;
  },


  addPlayer(firstName, lastName, age) {
  let player = {
  firstName: firstName,
    lastName: lastName,
    age: age
    };
    this.players.push(player);
  },

addGame(opponent, teamPoints, opponentPoints) {
    let game = {
      opponent: opponent,
      teamPoints: teamPoints,
      opponentPoints: opponentPoints
    };
    this.games.push(game);
  }  
};

team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Bugs', 'Bunny', 76);
team.addGame('Titans', 100, 98);
console.log(team.games);


The issue is actually your getter methods. In your functions you are using this.players and this.games which I presume is meant to work off of the getters. However your getters have the names _players and _games, therefore this.players and this.games returns undefined as they do not exist. You need to remove the underscore from your getter definitions and everything should work right.

1 Like

Thanks for your response, I tried using just “this.players” and “this.games”, however I still receive the same error message.

objects-team-stats/app.js:51
      this.players.push(player);}
                  ^

TypeError: Cannot read property 'push' of undefined

Sorry I should have been clearer, it’s the getter definitions here

That are the issue. When you’re referring to this.players, it’s not finding anything as these have an underscore in front of them.

1 Like