Clarifying something in this code

In the following code, I created a method called addPlayer…my question is, why must I create an object inside of the method, and why must I add three properties inside of it? Why would it not suffice to simply add a return of the three values?

const team = { _players: [ {firstName: 'Carlos', lastName: 'Tevez', age: 37}, {firstName: 'Iran', lastName: 'Lievanos', age: 25}, {firstName: 'Samir', lastName: 'Nasri', age: 37}, ], _games:[ {opponent: 'Barcelona', teamPoints: 3, opponentPoints: 1}, {opponent: 'Atletico Madrid', teamPoints: 4, opponentPoints: 5}, {opponent: 'Real Madrid', teamPoints: 5, opponentPoints: 4}, ], get players(){ return this._players }, get games(){ return this._games }, addPlayer(newFirstName, newLastName, newAge){ let player = { firstName: newFirstName, lastName: newLastName, age: newAge, }; this.players.push(player) }, addGame (newOpponent, newTeamPoints,newOpponentPoints){ let newGame = { opponent: newOpponent, teamPoints: newTeamPoints, opponentPoints: newOpponentPoints }; }, }; team.addPlayer('Bugs', 'Bunny', 76) console.log(team.players) team.addGame('Granada', 4, 4) console.log(

All the previous objects that you are using for players has firstname, lastName, and age properties.

  _players: [
    {firstName: 'Carlos', lastName: 'Tevez', age: 37},
    {firstName: 'Iran', lastName: 'Lievanos', age: 25},
    {firstName: 'Samir', lastName: 'Nasri', age: 37},

so if you want to add another object to array, and the object works the same way, it should have the same properties.

In your code, the addPlayer method did not return anything; it pushed an object to ._players instead.

Also, functions only return one thing, so I don’t see how you would do a return of 3 values (unless those are in an object or array).

(I guess you could do return player in the addPlayer method, but the purpose of the method is to add a player, not to return stuff.)

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.