JavaScript_Team Stats


  • 配列、オブジェクト(setter、getterなど)を使用してチーム統計
  • を作成する.
  • チーム内にプレイヤー(firstname,lastname,age)とゲーム(相手,teampoints,oponentpoints)
  • を設定する
  • 選手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 } 
      
      */