Team Stats: players.push error

Hi,

I’ve been working on the Team Stats project and I couldn’t solve this error:

this.players.push(player);
^^^^
SyntaxError: Unexpected token this
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
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)

Here’s the link to the project:
https://www.codecademy.com/courses/introduction-to-javascript/projects/team-stats

Difficult to say without seeing the code. Please copy paste your full code to the forum

the link checks for authenticated/logged in user. So anyone clicking the link will see there own code/progress in the lesson

Sorry, this my first time creating a topic.

Here is the code:

const team = {

   _players: [

     {

       firstName: 'Ivan',

       lastName: 'Cheban', 

       age: 10

     },

     {

       firstName: 'Nikita',

       lastName: 'Boychev',

       age: 11

     },

     {

       firstName: 'Eva',

       lastName: 'Bogoslov',

       age: 7

     }

   ],

   _games: [

     {

      opponent: 'Erik',

      teamPoints: 17,

      opponentPoints: 16

     },

     {

      opponent: 'Igor',

      teamPoints: 14,

      opponentPoints: 17

     },

     {

      opponent: 'Timothy',

      teamPoints: 11,

      opponentPoints: 7

     },

   ],

    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);

   }

}

team.addPlayer('Andrey', 'Mirchev', 11)

console.log(team.players)

with the error at the start of a line, always check the line above as well:

     let player = {

       firstName: firstName,

       lastName: lastName,

       age: age

     },  <-- talking about this comma here 

you create an object (player), yet after you close the object (}) you added a comma, why?

I can highly recommend to look at some of the topics in section:

https://discuss.codecademy.com/c/community/getting-started/1754

which include formatting code (so code indent and strings show properly)

Removing the comma removed the error but now I have this error:

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

TypeError: Cannot read property ‘push’ of undefined
at Object.addPlayer (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:48:16)
at Object. (/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:51: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)

I get a very different error now:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:73
      return this.players;
                 ^

RangeError: Maximum call stack size exceeded

could you copy and paste your entire code again? This time with format please? Have you tried troubleshooting/debugging this new issue? Any idea what caused it?

const team = {

   _players: [

     {

       firstName: 'Ivan',

       lastName: 'Cheban', 

       age: 10

     },

     {

       firstName: 'Nikita',

       lastName: 'Boychev',

       age: 11

     },

     {

       firstName: 'Eva',

       lastName: 'Bogoslov',

       age: 7

     }

   ],

   _games: [

     {

      opponent: 'Erik',

      teamPoints: 17,

      opponentPoints: 16

     },

     {

      opponent: 'Igor',

      teamPoints: 14,

      opponentPoints: 17

     },

     {

      opponent: 'Timothy',

      teamPoints: 11,

      opponentPoints: 7

     },

   ],

    get players() {

      return this.players;

    },

    get games() {

      return this.games;

    },

   addPlayer(firstName, lastName, age) {

     let player = {

       firstName: firstName,

       lastName: lastName,

       age: age

     }

    this.player.push(player)

   }

}

team.addPlayer('Andrey', 'Mirchev', 11)

console.log(team.players)

you also made another changed:

this.players.push(player); // old 
this.player.push(player) // new

Why did you make this change? You want to push the player you players array.

you have a problem with the getter:

    get players() {

      return this.players;

    },

If we check the docs on getter:

The get syntax binds an object property to a function that will be called when that property is looked up.

so you create a property with bound function (using get), then within the getter you use this property, which will endlessly invoking the bound function. Resulting in the error I had.

I also don’t know what you did with formatting your code, but looked like you use block quote while you should use preformatted text

Thanks for helping, I changed it back to

this.players.push(player);

and it helped.
I solved the RangeError myself.

It was saying it wasn’t defined because you were doing push(player) into players which is not defined. You defined in the beginning of the team Object _players.

So the addPlayer() Method should return:

this._players(player);

not

this.players(player);