Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 26, 2020 18:30
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 codecademydev/9cf7291b3d36ef062884f8b9ec7f4ccb to your computer and use it in GitHub Desktop.
Save codecademydev/9cf7291b3d36ef062884f8b9ec7f4ccb to your computer and use it in GitHub Desktop.
Codecademy export
const team = {
get players() {
return this._players;
},
get games() {
return this._games;
},
_players: [
{firstName: "Peter", lastName: "Svärd", age: 38},
{firstName: "Alice", lastName: "Svärd", age: 4},
{firstName: "Sara", lastName: "Hjertsdotter", age: 31},
{firstName: "Ellen", lastName: "Svärd", age: 1}],
_games: [
{opponent: "HV 71", teamPoints: 1, opponentPoints: 4},
{opponent: "Leksand", teamPoints: 5, opponentPoints: 4},
{opponent: "Färjestad", teamPoints: 5, opponentPoints: 3}],
addPlayer(firstName, lastName, age) {
const player = {
firstName: firstName,
lastName: lastName,
age: age,
};
this._players.push(player);
},
addGame(oppponent, teamPoints, opponentPoints){
const game = {
opponent: opponent,
teamPoints: teamPoints,
opponentPoints: opponentPoints,
};
this._games.push(game);
},
aveAge() {
var ageArr = []
for (let i = 0; i < this._players.length; i++) {
ageArr.push(this.players[i].age);
};
let sumAge = ageArr.reduce((a, b) => a + b, 0);
let teamAveAge = sumAge / ageArr.length;
let teamAveAge_result = teamAveAge.toFixed(2);
return `Average age in the team: ${teamAveAge_result} years`;
},
listPlayers () {
for (let i in team._players){
console.log(`${team._players[i].firstName} ${team._players[i].lastName} - age: ${team._players[i].age}`);
};
}
};
team.addPlayer("Steph", "Curry", 28);
team.addPlayer("Lisa", "Leslie", 44);
team.addPlayer("Bugs", "Bunny", 76);
team.addPlayer("Martin", "Svärd", 38);
console.log(team._players);
console.log(team.listPlayers());
console.log(team.aveAge());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment