Codecademy Javascript Team Stats Project

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

Link to the exercise: Learn JavaScript | Codecademy

Error Message:
/home/ccuser/workspace/learn-javascript-objects-team-stats-revised/app.js:2
_players : [{firstName: ‘Dak’, lastName: ‘Prescott’, age: 29}, {firstName: ‘Ezekiel’, lastName: ‘Elliott’, 27}, {firstName: ‘CeeDee’, lastName: ‘Lamb’, 23}],
^

SyntaxError: Unexpected token ‘}’
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions…js

const team = {
  _players : [{firstName: 'Dak', lastName: 'Prescott', age: 29}, {firstName: 'Ezekiel', lastName: 'Elliott', 27}, {firstName: 'CeeDee', lastName: 'Lamb', 23}],
  
  _games : {opponent: 'Washington', teamPoints: 6, opponentPoints: 26} {opponent: "Tennessee", teamPoints: 27, opponentPoints: 13} {opponent: 'Philadelphia', teamPoints: 40, opponentPoints: 34}
}; 

get players(){
  return this._players
} 

Firstly, where did I go wrong? I thought my syntax was fine.

Secondly I have a question about task 5

per the instructions: Create a getter method called players to retrieve the _players property. Inside the getter method, return the _players property.

For the scope of this project, we won’t need to create setter methods, because we don’t want anyone to change the data saved to the properties.

my question: what are looking to do with this get method? Grab the properties of what? The first name and last name and age?

hey I think you forgot to put the array in and missed some commas
try this

const team = {
_players : [{firstName: ‘Dak’, lastName: ‘Prescott’, age: 29}, {firstName: ‘Ezekiel’, lastName: ‘Elliott’, 27}, {firstName: ‘CeeDee’, lastName: ‘Lamb’, 23}],

_games : [{opponent: ‘Washington’, teamPoints: 6, opponentPoints: 26} ,{opponent: “Tennessee”, teamPoints: 27, opponentPoints: 13}, {opponent: ‘Philadelphia’, teamPoints: 40, opponentPoints: 34}]
},
get players(){
return this._players
} }

As @kuskus2468800273 mentioned, you are missing some brackets and commas.

You need to separate the objects in _games by commas and to wrap them in square brackets []

_games: [ {...}, {...}, {...} ]

Also, in the players array, a couple of entries in your objects haven’t been given keys e.g.
Instead of {..., ..., 27}, it should be {..., ..., age: 27}

_players is an internal property of the team object. It is an array of objects containing player details such as name and age. We could print out (or access this array) with a statement like console.log(team._players). But it is generally a good idea to not allow direct access to internal properties of an object. By using getters and setters, we can limit how access and modifications are allowed. Within the object, we may use underscores or different variable names, but the user doesn’t need to get bogged down in those details. If we change the internal implementations of the properties but retain the getters and setters, then the user remains unaffected by the changes. We can carry out validations. We can change the format in which the data held in the properties is made accessible to the user.

get players()
    return this._players;

In this case, the getter may be fairly trivial. To access information about the players, we can just use the name of the getter.

Instead of having the user work with the internal names of properties

console.log(team._players);

or invoke functions,

console.log(team.players());

the user can just use the name of the getter to access the data,

console.log(team.player);

In this particular exercise, the above will just print the array of player details (first name, last name, age).

Even though this stackoverflow thread is about Java, similar reasoning can be used for JavaScript getters and setters: Why use getters and setters/accessors?

Another thread, Why use getters and setters in JavaScript? - Stack Overflow

1 Like