Team Stats: Objects, Type Error

Hello,
I am working on the team stats project and so far I’ve had no issues. Although, now I am on the second to last step and I keep getting an error that has me stumped.
I am trying to push newly added elements into an existing array, but keep getting an error saying: “Type Error: cannot read property ‘push’ of undefined”. It is referring to my line: this.games.push(game); I cannot understand the issue considering the exact same formatted line works for the players array. Even if I change the code to “this._games.push(game)”, it still doesn’t work. I am lost on this one.

Here is my code:

const team = { _players: [ {firstName: 'Nick', lastName: 'Cannon', age: 89}, {firstName: 'Juan', lastName: 'Alsojuan', age: 69}, {firstName: 'Carlos', lastName: 'Carlosos', age: 42} ], __games: [ {opponent: 'The Losers', teamPoints: 50, opponentPoints: 59}, {opponent: 'KobraKai', teamPoints: 50, opponentPoints: 52}, {opponent: 'BushLeague', teamPoints: 50, opponentPoints: 59} ], 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(opponentName, teamPoints, opponentPoints){ let game = { opponentName: opponentName, teamPoints: teamPoints, opponentPoints: opponentPoints } this.games.push(game) } }; team.addPlayer('steph', 'curry', 28) team.addPlayer('Lisa', 'Leslie', 44) team.addPlayer('Bugs', 'Bunny', 76) //console.log(team.players) team.addGame('Alphas', 50, 48) team.addGame('Betas', 50, 42) team.addGame('Deltas', 50, 58) //console.log(team.games)
const team = {

  _players: [

    {firstName: 'Nick', lastName: 'Cannon', age: 89},

    {firstName: 'Juan', lastName: 'Alsojuan', age: 69},

    {firstName: 'Carlos', lastName: 'Carlosos', age: 42}

  ],

  __games: [

    {opponent: 'The Losers', teamPoints: 50, opponentPoints: 59},

    {opponent: 'KobraKai', teamPoints: 50, opponentPoints: 52},

    {opponent: 'BushLeague', teamPoints: 50, opponentPoints: 59}

  ],

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(opponentName, teamPoints, opponentPoints){

  let game = {

    opponentName: opponentName,

    teamPoints: teamPoints,

    opponentPoints: opponentPoints

  }

  this.games.push(game)

}

};

team.addPlayer('steph', 'curry', 28)

team.addPlayer('Lisa', 'Leslie', 44)

team.addPlayer('Bugs', 'Bunny', 76)

//console.log(team.players)

team.addGame('Alphas', 50, 48)

team.addGame('Betas', 50, 42)

team.addGame('Deltas', 50, 58)

//console.log(team.games)

Double underscore is the issue.

WOW, your attention to detail is very much appreciated sir, thank you.

1 Like

You’re welcome. Some times it pops out, others it can be as plain as the nose on one’s face and we are blind to it. That’s human.

True, sometimes the obvious is over looked, especially if you’ve been staring at the same code for over an hour.

1 Like