Advertisement
makispaiktis

8. Codecademy Intro - Team stats

Oct 21st, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Factory Function of players and games - Constructors
  2. // Destructuring assignment
  3. function Player(firstName, lastName, age){
  4.     return {
  5.         firstName, lastName, age
  6.     }
  7. }
  8.  
  9. function Game(opponent, teamPoints, opponentPoints){
  10.     return {
  11.         opponent, teamPoints, opponentPoints
  12.     }
  13. }
  14.  
  15. // Create objects - instances
  16. player1 = Player('Cole', 'Palmer', 20);
  17. player2 = Player('Reece', 'James', 25);
  18. player3 = Player('Christian', 'Nkunku', 28);
  19. game1 = Game('Wolves', 1, 1);
  20. game2 = Game('Crystal Palace', 3, 0);
  21. game3 = Game('Liverpool', 0, 3);
  22.  
  23. // Create an object
  24. const team = {
  25.     // Attributes
  26.     _players : [player1, player2, player3],
  27.     _games : [game1, game2, game3],
  28.     // Getters (there will be NO setters)
  29.     get players(){
  30.         return this._players;
  31.     },
  32.     get games(){
  33.         return this._games;
  34.     },
  35.     // Methods
  36.     addPlayer(newFN, newLN, newAge){
  37.         player = Player(newFN, newLN, newAge);
  38.         this._players.push(player);
  39.     },
  40.     addGame(newO, newTP, newOP){
  41.         game = Game(newO, newTP, newOP);
  42.         this._games.push(game);
  43.     },
  44.     status(){
  45.         console.log(this._players);
  46.         console.log(this._games);
  47.     }
  48. }
  49.  
  50. // MAIN FUNCTION
  51. team.addPlayer('Enzo', 'Fernandez', 22);
  52. team.addGame('Manchester City', 3, 0);
  53. team.status();
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement