I did the “Team Stats” project but I also added two extra functions: “calcWAndL()” and “logPlayers()”.
I made “logPlayers()” so i could read the players more easily when printed, and I made “calcWAndL()” so i could calculate wins and loses and see which games I won or lost. Here is my code:
const teamPatriots = {
_players:
[
{
firstName: 'Furgie',
lastName: 'Wurgie',
age: 13,
},
{
firstName: 'Ryan',
lastName: 'Tomich',
age: 13.5,
},
{
firstName: 'Scottie',
lastName: 'Wurgie',
age: 2,
},
],
_games: [
{
opponent: 'Phoenixes',
teamPoints: 18,
opponentPoints: 22,
},
{
opponent: 'Cobras',
teamPoints: 24,
opponentPoints: 13,
},
{
opponent: 'Tigers',
teamPoints: 19,
opponentPoints: 17,
},
],
get players() {
return this._players;
},
get games() {
return this._games;
},
addPlayer(firstName, lastName, age) {
let player = {
firstName: firstName,
lastName: lastName,
age: age,
};
this.players.push(player);
},
addGame(opponent, teamPoints, opponentPoints) {
let game = {
opponent: opponent,
teamPoints: teamPoints,
opponentPoints: opponentPoints,
};
this.games.push(game);
},
calcWAndL() {
let wins = 0;
let loses = 0;
let ties = 0;
for (let i = 0; i < teamPatriots._games.length; i += 1) {
console.log(teamPatriots._games[i].opponent);
if (teamPatriots._games[i].teamPoints > teamPatriots._games[i].opponentPoints) {
console.log('You won');
console.log('');
wins += 1;
} else if (teamPatriots._games[i].teamPoints < teamPatriots._games[i].opponentPoints) {
console.log('You lost');
console.log('');
loses += 1;
} else {
console.log('You tied');
console.log('');
ties += 1;
}
}
console.log(`Wins: ${wins}`);
console.log(`Loses: ${loses}`);
console.log(`Ties: ${ties}`);
console.log('');
},
};
function logPlayers() {
for (let i = 0; i < teamPatriots._players.length; i += 1) {
console.log(`Player ${i + 1}`);
console.log(`First Name: ${teamPatriots._players[i].firstName}`);
console.log(`Last Name: ${teamPatriots._players[i].lastName}`);
console.log(`Age: ${teamPatriots._players[i].age}`);
console.log('');
}
}
teamPatriots.addPlayer('Stephen', 'Curry', 28);
teamPatriots.addPlayer('Lisa', 'Leslie', 44);
teamPatriots.addPlayer('Bugs', 'Bunny', 76);
teamPatriots.addGame('Some Crappy Team', 999, 0);
teamPatriots.addGame('Unstopable Team', 0, 9999);
teamPatriots.addGame('Medeocer Team', 20, 20);
teamPatriots.calcWAndL();
logPlayers();
This is what it printed:
Phoenixes
You lost
Cobras
You won
Tigers
You won
Some Crappy Team
You won
Unstopable Team
You lost
Medeocer Team
You tied
Wins: 3
Loses: 2
Ties: 1
Player 1
First Name: Furgie
Last Name: Wurgie
Age: 13
Player 2
First Name: Ryan
Last Name: Tomich
Age: 13.5
Player 3
First Name: Scottie
Last Name: Wurgie
Age: 2
Player 4
First Name: Stephen
Last Name: Curry
Age: 28
Player 5
First Name: Lisa
Last Name: Leslie
Age: 44
Player 6
First Name: Bugs
Last Name: Bunny
Age: 76
If you think of a better way to do this please reply and let me know.