Team stats addplayer error

Hi I am doing the Team stats project and I keep getting this error:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:39
 addPlayer(firstName, lastName, age) {
 ^^^^^^^^^
SyntaxError: Missing initializer in const declaration
    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)

I’ve added a const to the addplayer but that doesn’t work. I checked the video tutorial and my code seems quite similar. Am I missing something from the code or do I need to declare it earlier in the code?

 addPlayer(firstName, lastName, age) {
    let player = {
      firstName: firstName,
      lastName: lastName,
      age: age
    };

    this.players.push(player);

Please post a link to the exercise, and your complete code.

https://www.codecademy.com/courses/introduction-to-javascript/projects/team-stats

const team = {
_players: [
  {firstName: 'John',
  lastName: 'Beatle',
  age: 30},
  {firstName: 'Paul',
  lastName: 'Beatle',
  age: 31},
  {firstName: 'George',
  lastName: 'Beatle',
  age: 32}
],
_games: [
  {
  opponent: 'Broncos',
  teamPoints: 42,
  opponentPoints: 27
},
{
  opponent: 'Monkeys',
  teamPoints: 45,
  opponentPoints: 29
},
{
  opponent: 'Beach Boys',
  teamPoints: 50,
  opponentPoints: 35
}
],

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(opp, myPts, oppPts) {
    const game = {
      opponent: opp,
      teamPoints: myPts,
      opponentPoints: oppPts
    };
    this.games.push(game);
  }
team.addGame('Titans', 100, 98);
  
  
  team.addPlayer('Steph', 'Curry', 28);
  team.addPlayer('Lisa', 'Leslie', 44);
  team.addPlayer('Bugs', 'Bunny', 76);

console.log(team);

Where is the closing brace on your team object?

Those are circular references. Should be, this._ ...

Got it, Thanks!

But now it is saying:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:31
get players(){
    ^^^^^^^
SyntaxError: Unexpected identifier
    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)

So I don’t need the return on the getters?

There is no setter so your need to write,

this._players.push(...)

I already have the setter’s after the addPlayer section.

I’ve _ but that doesn’t make a difference.

I don’t see the setters, above. Can you re-post your so far corrected code in a new reply?

Have you learned about classes or constructors, yet?

I am currently learning about classes and constructors, but this project is before that subject so they shouldn’t be needed for a project before you’ve learnt about them.

const team = {
_players: [
  {firstName: 'John',
  lastName: 'Beatle',
  age: 30},
  {firstName: 'Paul',
  lastName: 'Beatle',
  age: 31},
  {firstName: 'George',
  lastName: 'Beatle',
  age: 32}
],
_games: [
  {
  opponent: 'Broncos',
  teamPoints: 42,
  opponentPoints: 27
},
{
  opponent: 'Monkeys',
  teamPoints: 45,
  opponentPoints: 29
},
{
  opponent: 'Beach Boys',
  teamPoints: 50,
  opponentPoints: 35
}
],
}
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(opp, myPts, oppPts) {
    const game = {
      opponent: opp,
      teamPoints: myPts,
      opponentPoints: oppPts
    };
    this._games.push(game);
  }
team.addGame('Titans', 100, 98);
  
  
  team.addPlayer('Steph', 'Curry', 28);
  team.addPlayer('Lisa', 'Leslie', 44);
  team.addPlayer('Bugs', 'Bunny', 76);

console.log(team);

The setters are below ‘addPlayer’ and ‘addGame’.

The closing brace above the getter is closing the team object. Remove that brace and place it above,

team.addGame('Titans', 100, 98);

Ok, I’ve removed the closing brace. And that seemed to clear that error but what am I doing wrong with:

team.addGame('Titans', 100, 98);

I followed the video for that part but doesn’t work when I do it. Am I missing something?

Ok, I’ve gone through my code again but now I am getting this error:

/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:60
});
 ^
SyntaxError: Unexpected token )
    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)

I haven’t got a line 60 in my code. it only goes up to line 59. :confused:

What does line 59 read?

You could copy and save your code on the side (your text editor) and start from scratch, focusing on first the players, and then the games.

const team = {
  _players = [],
  _games = [],
  get players () {

  },
  get games () {

  },
  addPlayer () {

  },
  addGame () {

  }
}

team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);

We can see there will be three parameters in the addPlayer method so we can add them in.

I should walk back what I said earlier. The addPlayer method CAN use the getter, alone.

addPlayer (first, last, age) {
  this.players.push({first, last, age});  // object destructuring
  console.log(`Player, ${first} ${last} added to team.`);
},

When we run the three lines above, we get this in the display…

Player, Steph Curry added to team.
Player, Lisa Leslie added to team.
Player, Bugs Bunny added to team.

Now the addGame method,

addGame (opp, us, them) {
  this.games.push({opp, us, them});  // object destructuring
  console.log(`Game, ${us} to ${them} vs ${opp} added to games.`);
},

Now when we run these lines,

team.addGame('Sharks', 7, 1);
team.addGame('Kings', 6, 2);
team.addGame('Ducks', 4, 3);

we get this in the display…

Game, 7 to 1 vs Sharks added to games.
Game, 6 to 2 vs Kings added to games.
Game, 4 to 3 vs Ducks added to games.

From here we can proceed to the next steps in the project.

Line 59 reads:

console.log(team.player);

I haven’t got a }); anywhere that I can see.

I’ve run across this error message before, just in the past couple of weeks but I forget how we solved it. Will need to dig through a few hundred posts to find it.

In the meantime, I have this working in a sandbox…

const team = {
  _players: [],
  _games: [],
  get players(){
    return this._players
  },
  get games(){
    return this._games
  },
  addPlayer(firstName, lastName, age) {
    const player = {
      firstName: firstName,
      lastName: lastName,
      age: age
    }
    this.players.push(player)
  },
  addGame(opp, myPts, oppPts) {
    const game = {
      opponent: opp,
      teamPoints: myPts,
      opponentPoints: oppPts
    };
    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('Sharks', 7, 1);
team.addGame('Kings', 6, 2);
team.addGame('Ducks', 4, 3);
console.log(team.games);

This is the output to the console…

[ { firstName: 'Steph', lastName: 'Curry', age: 28 },
  { firstName: 'Lisa', lastName: 'Leslie', age: 44 },
  { firstName: 'Bugs', lastName: 'Bunny', age: 76 } ]
[ { opponent: 'Sharks', teamPoints: 7, opponentPoints: 1 },
  { opponent: 'Kings', teamPoints: 6, opponentPoints: 2 },
  { opponent: 'Ducks', teamPoints: 4, opponentPoints: 3 } ]

Thank you so much for your help. Does the error message mean that I haven’t closed somewhere off?

1 Like

For all the time I spent on the other member’s problem you’d think I would remember what it boiled down to. Just now it’s escaping me.


Found it…

Rock, Paper, Scissors exercise

That was 900 posts ago (June 4).

Ah, so it was a non closed function then!

Thank you so much :slight_smile:

1 Like