Team Stats: Getter Methods

Good afternoon. I worked on the Team Stats project (https://www.codecademy.com/courses/introduction-to-javascript/projects/team-stats) and got stuck around creating the getter methods section. I managed to unstick myself by creating getter methods for each individual player and each game. However, this created essentially six different getter methods based on the three players and three games. Is there a way to create one getter method in total for all three player and game properties? When I tried to make one getter method for each, it would only log my first if statement rather than the if plus the other two else if statements. For comparison sake, here’s my work-around code with the six different getter methods:

// Player 1 corresponds to Index 0
  get player1() {
    if ((this._players[0].firstName === 'Deebo') && (this._players[0].lastName === 'Samuel') && (this._players[0].age < 100)) {
      console.log(`Name: ${this._players[0].firstName} ${this._players[0].lastName} 
Age: ${this._players[0].age}`);
    } else {
      console.log('This player does not play for the San Francisco 49ers.');
    } // Deebo Samuel getter method
  },
 // Player 2 corresponds to Index 1
 get player2() {
    if ((this._players[1].firstName === 'Trey') && (this._players[1].lastName === 'Lance') && (this._players[1].age < 100)) {
      console.log(`Name: ${this._players[1].firstName} ${this._players[1].lastName} 
Age: ${this._players[1].age}`);
    } else {
      console.log('This player does not play for the San Francisco 49ers.');
    } // Trey Lance getter method
 },
 // Player 3 corresponds to Index 2
 get player3() {
    if ((this._players[2].firstName === 'Fred') && (this._players[2].lastName === 'Warner') && (this._players[2].age < 100)) {
      console.log(`Name: ${this._players[2].firstName} ${this._players[2].lastName} 
Age: ${this._players[2].age}`);
    } else {
      console.log('This player does not play for the San Francisco 49ers.');
    } // Fred Warner getter method
 },
 // Game 1 corresponds to Index 0
 get game1() {
   if ((this._games[0].opponent === 'Dallas Cowboys') && (this._games[0].teamPoints < 100) && (this._games[0].opponentPoints) < 100) {
     console.log(`Opponent: ${this._games[0].opponent}
Final Score: 49ers ${this._games[0].teamPoints}, Cowboys ${this._games[0].opponentPoints}`);
   } else {
     console.log('This game did not occur.');
   } // NFC Wild Card Round getter method
 },
 // Game 2 corresponds to Index 1
 get game2() {
   if ((this._games[1].opponent === 'Green Bay Packers') && (this._games[1].teamPoints < 100) && (this._games[1].opponentPoints) < 100) {
     console.log(`Opponent: ${this._games[1].opponent}
Final Score: 49ers ${this._games[1].teamPoints}, Packers ${this._games[1].opponentPoints}`);
   } else {
     console.log('This game did not occur.');
   } // NFC Divisional Round getter method
 },
 // Game 3 corresponds to Index 2
 get game3() {
   if ((this._games[2].opponent === 'Los Angeles Rams') && (this._games[2].teamPoints < 100) && (this._games[2].opponentPoints) < 100) {
     console.log(`Opponent: ${this._games[2].opponent}
Final Score: Rams ${this._games[2].opponentPoints}, 49ers ${this._games[2].teamPoints}`);
   } else {
     console.log('This game did not occur.');
   } // NFC Championship Game getter method
 },

versus what I attempted to do originally by having one getter method for all three properties of the _players array:

get players() {
   if ((this._players[0].firstName === 'Deebo') && (this._players[0].lastName === 'Samuel') && (this._players[0].age , 100)) {
     console.log(`Name: ${this._players[0].firstName} ${this._players[0].lastName}
Age: ${this._players[0].age}`);
   } else if ((this._players[1].firstName === 'Trey') && (this._players[1].lastName === 'Lance') && (this._players[1].age , 100)) {
     console.log(`Name: ${this._players[1].firstName} ${this._players[1].lastName}
Age: ${this._players[1].age}`);
   } else if ((this._players[2].firstName === 'Fred') && (this._players[2].lastName === 'Warner') && (this._players[2].age , 100)) {
     console.log(`Name: ${this._players[2].firstName} ${this._players[2].lastName}
Age: ${this._players[2].age}`);
 } else {
   console.log('This player is not a part of the San Francisco 49ers.');
 }
 },

If there’s a way to get all three properties in one getter method, that would cut down the line of code by like 75%. But in the end, does it even matter? Will other programmers care that the code is longer than it has to be as long as it gets the same result?

Yes, absolutely: The easier to read, the better. Imagine you want to work on this code half a year later, how long will it take to understand what you’ve done there?
What if you had a program which is connected to a website and a website visitor adds a player called Sven after having added 99 other players? How would you get this player?
The getter should get properties by certain criteria and be generic.
If you want to get a certain player by name, write a method that takes the name as argument and returns the relevant information like:

getPlayer(name) {
 return ...
}

You can use array methods like .find() to iterate through your array of players:

return this._players.find(...)

The method would be called like this:

const svenInfo = team.getPlayer('Sven');
1 Like