Skip to content

Instantly share code, notes, and snippets.

@digicommons
Created January 13, 2022 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digicommons/fef14bdee1e8841a0a6f758f225208e2 to your computer and use it in GitHub Desktop.
Save digicommons/fef14bdee1e8841a0a6f758f225208e2 to your computer and use it in GitHub Desktop.
Team Stats [Codecademy]
const team = {
_players: [
{firstName: 'Max', lastName: 'Blah', age: 16},
{firstName: 'Maria', lastName: 'Blub', age: 21},
{firstName: 'Josef', lastName: 'Blii', age: 26},
],
_games: [
{opponent: 'Sparta', teamPoints: 0, opponentPoints: 0},
{opponent: 'Slavia', teamPoints: 1, opponentPoints: 2},
{opponent: 'Viktoria', teamPoints: 2, opponentPoints: 1},
],
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};
this.games.push(game);
},
// This method returns 'undefined' as last item
displayPlayers() {
for (let i = 0; i < this._players.length; i++) {
const firstName = team._players[i].firstName;
const lastName = team._players[i].lastName;
const age = team._players[i].age;
console.log(`${firstName} ${lastName} is ${age} years old`);
}
}
}
// (...)
console.log(team.displayPlayers());
/* Console:
Max Blah is 16 years old
Maria Blub is 21 years old
Josef Blii is 26 years old
undefined
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment