Team Stats - how to get score and number of games

Hey!

I finished the Team Stats project (https://www.codecademy.com/courses/introduction-to-javascript/projects/team-stats), however I’d like to add the average score of the team and also the total number of games it has played.
Honestly don’t know how to start, objects got me confused…

This is my code so far:

const team = {
_players: [
{firstName: ‘Bob’,
lastName: ‘Smith’,
age: 20},
{firstName: ‘John’,
lastName: ‘Williams’,
age: 33},
{firstName: ‘Dave’,
lastName: ‘Brown’,
age: 28}
],
_games: [
{opponent: ‘Arsenal’,
teamPoints: 30,
opponentPoints: 42},
{opponent: ‘Chealsea’,
teamPoints: 27,
opponentPoints: 35},
{opponent: ‘Juventus’,
teamPoints: 30,
opponentPoints: 28}
],
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(‘Lisa’, ‘Leslie’, 44);
team.addPlayer(‘Bugs’, ‘Bunny’, 76);

team.addGame(‘Manchester’, 50, 40);
team.addGame(‘Liverpool’, 50, 100);
team.addGame(‘Real Madrid’, 50, 15);

console.log(team._players);
console.log(team._games);

Thanks for the comment–I’m also learning objects and that was a good challenge. I also think objects are pretty confusing–this was the first section that I had to go back over a second time because I wasn’t getting it.

I was able to put something together that should work. Here’s the method I came up with for averaging points and returning the number of games:

 averagePoints() {
  holderArray = [];
  for (const property in this.games) {
  holderArray.push(this.games[property].teamPoints)
    }
   const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length;
  console.log(`There have been ${holderArray.length} games this season, with an average of ${arrAvg(holderArray)} points per game.`)
  }

The first line declares a holder array that we’ll need to collect the scores. Then I used a for…in object loop to go through the _games array, grab the points in teamPoints, and push them to the holder. I added the points up using the .reduce() method…I’m not very good with that method yet, so I used Google to find something that would output an average. Finally, I log everything when the method is called.

Here’s my full code if you need to look at it:

const team = {
  _players: [
    {
      firstName: 'Mark',
      lastName: 'McGwire', 
      age: 33
    }, 
    {
      firstname: 'Willie',
      lastname: 'McGee',
      age: 43
    }, 
    {
      firstname: 'Ozzie',
      lastname: 'Smith',
      age: 35
    }
    ],
  get players() {
    return this._players;
  },
  _games: [
    {
      opponent: 'Braves',
      teamPoints: 13,
      opponentPoints: 17
    },
    {
      opponent: 'Royals',
      teamPoints: 7,
      opponentPoints: 3
    },
    {
      opponent: 'Cubs',
      teamPoints: 25,
      opponentPoints: 0
    }
  ],
  get games() {
    return this._games;
  },
  addPlayer(firstName, lastName, age) {
    let newPlayer = {
      firstName,
      lastName,
      age
      }
    this._players.push(newPlayer);
  },
  addGame(opponent, teamPoints, opponentPoints) {
    let newGame = {
      opponent,
      teamPoints,
      opponentPoints
    }
    this._games.push(newGame);
  },
  averagePoints() {
  holderArray = [];
  for (const property in this.games) {
  holderArray.push(this.games[property].teamPoints)
    }
   const arrAvg = arr => arr.reduce((a,b) => a + b, 0) / arr.length;
  console.log(`There have been ${holderArray.length} games this season, with an average of ${arrAvg(holderArray)} points per game.`)
  }
};

team.averagePoints()

team.addGame('Orioles', 7 ,3) // testing

team.averagePoints()

-Adam

Hi Adam!

This works beautifully, thank you for explaining the lines. I was trying the reduce method but was having a hard time. Had to read your code multiple times, but it’s definitely clearer to me now!

I just didn’t understand why the “0” on “((a,b) => a + b, 0)”, is there a specific reason why you used it? I found it works ok without it, but not sure if I should still keep it there.

Thank you SO much

Glad it worked!

To tell you the truth, I’m not sure why the 0 is there either. It didn’t strike me as necessary when I first looked at it in what I Googled, but I didn’t think to take it out and see if the code would still run. I’m going to take it out of mine and see what happens. This will give me a reason to look back over the documentation for that method and see if I can understand it better…for me, the reduce method is one of the more complicated iterator methods.

I just checked my notes for the reduce method and: if you add a number after the coma, it will be added to the total (so if for example, the total score was 100 and you had written 50, instead of 0, the total would be 150, and that would be divided by the “arr.length”.

Also I find it, for me, really hard when they use “a” and “b” to explain this, I much prefer to write “accumulator”, I don’t know, just makes me understand better what’s being done. This is what I put in my code (just changed the words a bit):

const averageScore = score => score.reduce((accumulator, points) => accumulator + points) / score.length;

Thanks again, hope we master this method soon… haha

1 Like