Hey all! Here’s my question:
In an effort to “fail fast” some of my code, I wrote in a few if/else statements that would return error messages if certain properties within objects as well as arguments weren’t correct. For some reason though, these statements are causing other parts of my code to malfunction. Here’s what I wrote
const team = {
_players: [
{firstName: 'Harry',
lastName: 'Kane',
age: 25},
{firstName: 'Lucas',
lastName: 'Moura',
age: 27},
{firstName: 'Pablo',
lastName: 'Sanchez',
age: 11}
],
_games: [
{opponent: 'Arsenal',
teamPoints: 5,
opponentPoints: 0},
{opponent: 'Manchester United',
teamPoints: 3,
opponentPoints: 1},
{opponent: 'Chelsea',
teamPoints: 4,
opponentPoints: 2}
],
get players() {
if (this._players.firstName && this._players.lastName && this._players.age && typeof this._players.age === 'number' && typeof this._players.firstName === 'string' && typeof this._players.lastName === 'string') {
return this._players
} else {
return `Error: Name and age invalid`
}
},
get games() {
if (this._games.opponent && this._games.teamPoints && this._games.opponentPoints && typeof this._games.opponent === 'string' && typeof this._games.teamPoints === 'number' && typeof this._games.opponentPoints === 'number') {
return this._games
} else {
return `Error: Games and points are invalid`
}
},
addPlayer(firstName, lastName, age) {
if (typeof firstName === 'string' && typeof lastName === 'string' && typeof age === 'number' && firstName && lastName && age) {
let player = {
firstName,
lastName,
age,
}
this.players.push(player);
} else {
return `Something went wrong.`
}
}
};
team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);
console.log(team.players)
For some reason - my fail fast code is causing this error:
/home/ccuser/workspace/learn-javascript-objects-team-stats/app.js:48
this.players.push(player);
^
TypeError: this.players.push is not a function
When I remove my fail fast code, everything works fine. What is going on here? Thanks!