JavaScript_Team Stats

정세비·2021년 5월 25일
0

test

목록 보기
11/13
post-thumbnail
  • 배열, 객체(setter, getter 등)을 활용하여 team stats 만들기
  • team 내에 players(firstname, lastname, age) 와 games(opponent, teampoints, opponentpoints) 설정하기
  • 선수 3명과 게임 3개 추가하기
const team = {
  _players: [{
    firstName: 'Marco',
    lastName: 'Rossi',
    age: 28},
    {firstName: 'Ian',
    lastName: 'Williams',
    age: 34},
    {firstName: 'Jamie',
    lastName: 'Bowden',
    age: 21},
  ],

  _games: [{
    opponent: 'Dragon',
    teamPoints: 2,
    opponentPoints: 3},
    {opponent: 'Tiger',
    teamPoints: 6,
    opponentPoints: 4},
    {opponent: 'Zeus',
    teamPoints: 0,
    opponentPoints: 1},
  ],

  get players() {
    return this._players;
  },

  get games() {
    return this._games;
  },

  addPlayer(firstName, lastName, age) {
    let newPlayer = {
      firstName,
      lastName,
      age,
    };

    this._players.push(newPlayer);
  },

addGame(opponent, teamPoints, opponentPoints) {
  let newGame = {
    opponent,
    teamPoints,
    opponentPoints,
  };

  this._games.push(newGame);
}

};


team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);

team.addGame('Titans', 4, 1);
team.addGame('Hurricane', 0, 0);
team.addGame('Killer', 1, 7);

console.log(team.players);
console.log(team.games);


/* output:
 { firstName: 'Marco', lastName: 'Rossi', age: 28 },
  { firstName: 'Ian', lastName: 'Williams', age: 34 },
  { firstName: 'Jamie', lastName: 'Bowden', age: 21 },
  { firstName: 'Steph', lastName: 'Curry', age: 28 },
  { firstName: 'Lisa', lastName: 'Leslie', age: 44 },
  { firstName: 'Bugs', lastName: 'Bunny', age: 76 } 
 { opponent: 'Dragon', teamPoints: 2, opponentPoints: 3 },
  { opponent: 'Tiger', teamPoints: 6, opponentPoints: 4 },
  { opponent: 'Zeus', teamPoints: 0, opponentPoints: 1 },
  { opponent: 'Titans', teamPoints: 4, opponentPoints: 1 },
  { opponent: 'Hurricane', teamPoints: 0, opponentPoints: 0 },
  { opponent: 'Killer', teamPoints: 1, opponentPoints: 7 } 
  
  */
profile
파주

0개의 댓글