Here’s my code for the Team Stats Project:
Any feedback would be greatly appreciated.
Happy Coding.
Looks great! It’s always nice to see people working on their code and hopefully enjoying it! Keep up the great work!
hello.
i have a quick question please,
this._games.push(game);
this.players.push(player);.
how come you use the _ for games, but not for the players ?.
thank you!
Seems like you spotted a mistake. The preceding underscore is used to signal to other developers using your code that they shouldn’t access/edit a property directly but instead use the provided getters/setters. It should be:
this._players.push(player);
Happy coding.
Hey Fellow coders adding my team stats project code
const team = {
_players: [
{firstName: 'Siri', lastName: '■■■■', age: 28},
{firstName: 'Sam', lastName: 'fur', age: 25},
{firstName: 'chris', lastName: 'Pratt', age: 38}],
_games: [
{opponents: 'Phil', teamPoints: 20, opponentPoints: 10},
{opponents: 'Gemma', teamPoints: 11, opponentPoints: 20},
{opponents: 'Alice', teamPoints: 34, opponentPoints: 25}
],
get players() {
return this._players;
},
get games () {
return this._games;
},
//adding new player
addPlayer(newFirstName, newLastName, newAge) {
let player = {
firstName: newFirstName,
lastName: newLastName,
age: newAge
};
this._players.push(player);
},
//adding new games
addGame(newOpponent, newTeamPoints, newOpponentPoints) {
let game = {
opponents: newOpponent,
teamPoints: newTeamPoints,
opponentPoints: newOpponentPoints
};
this._games.push(game);
},
//total num of games played
get totalNumOfGames () {
let totalnum = this._games.length;
console.log("You have played total " + totalnum + " games!");
}
//Average points of Oppenents
/* get averagePoints () {
for(let points in )
} */
};
// outside object
team.addPlayer('Bugs', 'Bunny', 76);
team.addGame('Titans', 30, 4);
console.log(team._players);
console.log(team._games);
team.totalNumOfGames;
let totalNum = 0;
for (let i = 0; i <team._games.length; i++) {
totalNum += team._games[i].opponentPoints;
}
let averagePoints = totalNum/team._games.length;
console.log('Total Num:', totalNum);
console.log(Math.round(averagePoints));
Hey everyone, I have a question, currently this is my code up until Step 8 of this project:
onst team = {
_players: [
{ firstName: “Kobe”, lastName: “Bryant”, age: 38 },
{ firstName: “Tracy”, lastName: “McGrady”, age: 38 },
{ firstName: “Derrick”, lastName: “Rose”, age: 34 },
],
_games: [
{ opponent: “Lakers”, teamPoints: 35, opponentsPoints: 40 },
{ opponent: “Magic”, teamPoints: 34, opponentsPoints: 44 },
{ opponent: “Bulls”, teamPoints: 30, opponentsPoints: 45 },
],
get players() {
return this._players;
},
get games() {
return this._games;
},
addPlayer(newFirstName, newLastName, newAge) {
let player = {
firstName: newFirstName = ‘Bugs’,
lastName: newLastName = ‘Bunny’,
age: newAge = 76,
};
this.players.push(player);
},
};
team.addPlayer();
console.log(team.players);
I compared it to the Unstuck Video, so what would happen to this code block since I added Bugs Bunny in the addPlayer object’s property directly, vs placing it with team.addPlayer(‘Bugs’, ‘Bunny’, 76);
Will there be any problems I might occur?
It still logged this in the console:
[
{ firstName: ‘Kobe’, lastName: ‘Bryant’, age: 38 },
{ firstName: ‘Tracy’, lastName: ‘McGrady’, age: 38 },
{ firstName: ‘Derrick’, lastName: ‘Rose’, age: 34 },
{ firstName: ‘Bugs’, lastName: ‘Bunny’, age: 76 }
]
I think that fails if you’re trying to add another player that isn’t the same (meaning not Bugs Bunny) afterward.
I see! Thank you for the feedback!
I have different question in regards to this project
I wanted to leave game objects empty, so I can assign them later, but could not wrap my head around it. How can I do that?
You will need to elaborate a bit more on what is the desired behavior. Perhaps, share an example of what you want to do.
In the screenshot, the error is because in the key-value pair
opponent: opponent
a key named "opponent"
will be created. That is not a problem. But, the value that you are trying to assign to this key does not exist. There is no variable named opponent
, so you get a ReferenceError
warning that opponent
is not defined and therefore it is not possible to retrieve any value from this non-existent variable. You expect teamPoints
and opponentPoints
to hold number values, so you have initialized them as 0
. Similarly, you expect opponent
to hold string values, so you can initialize it as an empty string,
{
opponent: "",
teamPoints: 0,
opponentPoints: 0
}