Team Stats


#1

Excercise Link : - https://www.codecademy.com/courses/learn-javascript-objects/projects/team-stats?course_redirect=introduction-to-javascript

// This object stores information about our team.
const team = {
  // This stores information about our player.
  _players: [
    {
      firstName: 'Pannu',
      lastName: 'Singh',
      age: 12
    },
    
    {
      firstname: 'Sandy',
      lastName: 'Singh',
      age: 13
    },
    {
      firstName: 'Pardhan',
      lastname: 'Singh',
      age: 15
    }
  ],
  // This stores information abouts games and points.
  _games: [
    {
      opponent: 'kings 11',
      teamPoints : 4,
      opponentPoints: 2
    },
    {
      opponent: 'Delhi Knights',
      teamPoints :9,
      opponentPoints: 2
    },
    {
      opponent: 'Mumbai Indians',
      teamPoints : 2,
      opponentPoints: 6
    }    
  ],
  
  get _players () {
    return this.players;
  },
  
  get _games () {
    return this.games;
  },
  
  // This object help us add new players to our team.
  addPlayer(firstName, lastName, age) {
    const player = {
      firstName: firstName,
      lastName: lastName,
      age: age
    };
  this._players.push(player);
},
  // This function helps us add new games and point.
  addGame(opponentName, teamPoint, opponentPoint) {
    const game = { 
      opponent: opponentName,
      teamPoints : teamPoint,
      opponentPoints: opponentPoint      
    };
    this._games.push(game);
  }
};
// Below is the list of new players
team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);
team.addGame('Sea Hawks', 100, 98);
team.addGame('Surrey', 54, 51);
team.addGame('New York', 33, 22);
console.log(team());



Can someone please review my code?

I keep getting this error below!

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:53
    this.players.push(player);
                ^

TypeError: Cannot read property 'push' of undefined
    at Object.addPlayer (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:53:17)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:57:6)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

#2

Look at here

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

_players was the property of the team object. so you shouldn’t use the same name for function and what is this.players?

shouldn’t it be this._players?

same for the

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

and see here

console.log(team())

team is not a function so don’t use () for it.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.