Team Stats Question

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!

Did you try this?

this._players.push(...)
5 Likes

We’ve atleast made progress. It kicks back my fail fast from my get players() code. I believe this could be because my Objects in _players are actually within an array. So I may have to write my fail fast a little differently. Something like:

if (this._players[0].firstName…)

The issue with this is that I’d have to write out [0], [1], [2] so that my fail fast checks every object I have within the array. I’m sure I could iterate over this, maybe something like:

get players () {
	for (i = 0; i > this._players[i].firstName; i++) {
		if (this.players[i].firstName && this.players[i].firstName ==='string') {
			return this._players
				} else {
					return `Error`
				}
		}
};

And then I would continue this structure over this._players.lastName and this._players.age. Is this the correct way to think about it?

That being said - I’m still not clear why my fail fast would cause this.players.push(players) to malfunction??

I am confused as to why you would use fail checkers in your getters since thats the info already present. You would only require a checker in your addPlayer and addGame functions to filter out false inputs to the object. Right?

On top of that, in your last post you have added return inside a for loop, what does return do? It will break of the entire operation i.e. the getter.

2 Likes

It fails for possibly two reasons:

  • there is no defined attribute, players

  • players cannot be both plural and singular at the same time

    this._players.push(player)

I’m guessing that the unit on iterators precedes this getters and setters unit (perhaps not?) so an iterator will be simpler than a for loop.

First though, what is it you wish to be returned from the players getter? A list or a print out?

list = []
this._players.forEach(x => list.push(`${x.firstName} ${x.lastName}`)
return list.join('\n')

The return will be a string object that resembles a list of player’s names.

3 Likes

Good point on the fail checkers. I suppose the only reasoning could be to make sure that the objects that were already written were correct. I am still a code newbie, only been learning for a few weeks so any info like this is greatly appreciated!

My thinking behind the return inside the for loop is that this would be a fail checker that would iterate through the array of objects in _players and check that firstName in each object was both valid and a string. Like you mentioned before, this may be more applicable to the addPlayer function rather than the getter, and in which case - I would not need to iterate because my addPlayer does not care about the array. Am I thinking about this right?

Thank you for the reply! As janneslohmeijer mentioned before, I perhaps do not need a fail checker for my getter method. My thinking was that this would fail check any prior code written before, but if it’s been passed off to me, it’s safe to assume everything would have been correct.

Honestly, many of the solutions to the projects in the javascript lessons incorporate fail checkers, and it is a concept that was never specifically included in the lesson. So i had to do my own research on them and so that is probably why I am misunderstanding their use/syntax. But all of this is extremely helpful! It gets more clear every day.

Thank you for the list suggestion. I will keep this idea in mind! I believe the purpose of the return was meant to be a print out, but I will go back and double check. Thanks for being patient with a beginner!

You’re welcome.

So the statement would read,

console.log(team.players)
3 Likes

The moment the for loop encounters an instance of return, it breaks of not just that iteration check but the entire function addPlayer().

2 Likes