Can anyone help me find the average age of the players that I have in the Team Stats project? Code below

``Preformatted text```
const team = {

_players: [

{ lastName: "Balmer", firstName: "Matthias", age: 40 },

{ lastName: "Caramanolis", firstName: "Atahan", age: 29 },

{ lastName: "Tucson", firstName: "Sophie", age: 30 },

],

_games: [

{ opponent: "Giants", teamPoints: 36, opponentPoints: 27 },

{ opponent: "Jungs", teamPoints: 21, opponentPoints: 28 },

{ opponent: "49ers", teamPoints: 17, opponentPoints: 36 },

],

get players() {

return this._players;

},

get games() {

return this._games;

},

countPlayers() {

return this._players.length;

},

averageAge() {

let totalAge = 0;

for (let age = 0; age < this._players.length; age++) {

  console.log(this._players[age].age);

}

},

addPlayer(newFirstName, newLastName, newAge) {

let player = {

  firstName: newFirstName,

  lastName: newLastName,

  age: newAge,

};

this.players.push(player);

},

addGame(newOpponent, newTeamPoints, newOpponentPoints) {

let game = {

  opponent: newOpponent,

  teamPoints: newTeamPoints,

  opponentPoints: newOpponentPoints,

};

this.games.push(game);

},

};

team.addPlayer(“Bugs”, “Bunny”, 76);

team.addGame(“Titans”, 100, 98);

team.averageAge();

You seem to be using age as an index for iterating through the ._players array,
And the averageAge function does not return anything …

You would need to add the age of the player to totalAge in each iteration of the loop;
and, after the loop, divide the total age by the number of players to get the average age.

spoiler:

averageAge() {
  let totalAge = 0;
  const numberOfPlayers = this._players.length;
  for (let i = 0; i < numberOfPlayers; i++) {
    totalAge += this._players[i].age;
  }
  if (numberOfPlayers > 0) {
    const average = totalAge / numberOfPlayers;
    return average;
  }
},

dear @janbazant1107978602, thank you so much. You have really helped me understand the issue. Take care.