Why aren't the getters referenced in the lessons?


#1

Hello, I just finished the Team Stats project:
https://www.codecademy.com/courses/introduction-to-javascript/projects/team-stats

// step 1
const team = {
  // step 2
  _players: [
    // step 3
    {firstName: "Pablo",
		lastName: "Sanchez",
		age: 11},
    {firstName: "Binga",
		lastName: "Doonata",
		age: 12},
    {firstName: "Ragu",
		lastName: "Bananaha",
		age: 13},
    {firstName: "Ooschooch",
		lastName: "Humwhoha",
		age: 14}
  ],
  // step 2
  _games: [
    // step 4
    {opponent: "Broncos", teamPoints: 42, opponentPoints: 27},
    {opponent: "Red Skins", teamPoints: 35, opponentPoints: 18},
    {opponent: "Indians", teamPoints: 29, opponentPoints: 33}
  ],
	// step 6
  addPlayer (firstName, lastName, age) {
    const player = {
      firstName,
      lastName,
      age
    }
    this._players.push(player)
  },
	// step 8   
  addGame (opponent, teamPoints, opponentPoints){
    const game = {
      opponent, 
      teamPoints, 
      opponentPoints
    }
    this._games.push(game)
  }
};
// step 7
team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie,', 44);
team.addPlayer('Bugs', 'Bunny', 76);
console.log(team._players);
// step 9
team.addGame('Razors', 28, 28);
team.addGame('Dragons', 29, 41);
team.addGame('Meat Eaters', 33, 25);
console.log(team._games);

I noticed that the work created in Step 5 is never used in the program. “Create getter methods for your _players and _games keys”

	// step 5
  get players () {
  	return this._players;
	},
	// step 5
  get games () {
  	return this._games;
	},

QUESTION: Why is step 5 even in the program if nothing in the code makes reference to it? I watched the video by the way to make sure I created the program correctly.

I noticed the same thing in the prior project called “Meal Maker”
https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker . Step 4 says "
Create getter and setter methods for the appetizers, mains, and desserts properties." If you skip step 4, everything works fine. Twice now I have seen this.

Thanks for any insight.


#2

Your program will need getters and setters for both player and games:


  get players () {
    return this._players;
  },
  get games () {
    return this._games;
  },
  set players (player) {
    this._players.push(player)
  },
  set games (game) {
    this._games.push(game)
  },

Once you have those in place, try changing your addPlayer,

this._players.push(player);

to,

this.players = player;

and change this (in addGame),

this._games.push(game);

to,

this.games = game;

See where the getter and setter comes in? We first get the property, then set it, in one motion.

Also,

console.log(team._players);

becomes,

console.log(team.players);

and,

console.log(team._games);

becomes,

console.log(team.games);

The purpose of using an alias as our primary property names is to protect them (somewhat) from outside changes. They are not private as they are still accessible. Other authors working on the project will be able to see how they are to be accessed.


#3

When I made the change you suggested in step 6:

//this._players.push(player)
this.players = player; // try mtf’s suggestion

it no longer added the data specified in step 7:

// step 7
team.addPlayer(‘Steph’, ‘Curry’, 28);
team.addPlayer(‘Lisa’, ‘Leslie,’, 44);
team.addPlayer(‘Bugs’, ‘Bunny’, 76);
console.log(team._players);

In effect, making your suggested change, broke the program. :frowning:

My issue is with commenting out the getter in step 5. If I completely delete the code, nothing bad happens. The program doesn’t seem to need it.

// step 5 : BTW: this program runs fine without this step
get players () {
return this._players;
},
// step 5
get games () {
return this._games;
},

Perhaps the instructor just wants the student to create them in prep for future lessons?


Edit

whoa, just figured it out while working on a more advanced program. If in step 7, you change:

console.log(team._players); // does NOT reference the getter in step 5

to

console.log(team.players); // references the gettiner in step 5

The program gives the same results but, takes advantage of the getter created in step 5. I made the same change in step 9. Thanks for triggering me to go back and review this exercise. I really appreciate it. My progress is incredibly slow but, trying to be thorough else, i get clobbered in the quizzes. :slight_smile:


#5

The getter/setter concept is simple to understand but difficult to explain. They are very useful as we’ve seen demonstrated above. Assignments and polls are simple to write and read, and all the messy stuff is tucked away in the helper methods.

May I take it that you have resolved any issues with your code/lesson?


#6

yes, this is resolved. I’m well into the “Build a Library” project.