Team stats project


I am currently working on the Team Stats Project and i have completed my code but i seem to get an error when trying to run in (looks like a problem with the line this.players.push(player); )
I would appreciate an advice if you notice something

here is the link to the project:

here is my code

const team = {
  _players : [ {
  firstName: 'Pete',
  lastName: 'Wheeler',
  age : 54
}, {
  firstName: 'Mike',
  lastName: 'Wheiner',
  age : 42
}, {
  firstName: 'Mark',
  lastName: 'Rashford',
  age : 32
  _games: [
  opponent: 'Broncos',
  teamPoints: 42,
  opponentPoints: 27
  opponent: 'Lemur',
  teamPoints: 22,
  opponentPoints: 13
  opponent: 'Ashton',
  teamPoints: 14,
  opponentPoints: 21
  get _players (){
    return this._players
  get _games (){
    return this._games
  addPlayer(firstName, lastName, age) {
    let player = {
      firstName: firstName,
      lastName: lastName,
  addGame (opponentName, myPoints, oppPoints){
    const game = {
      opponnentName : opponnentName,
      myPoints : myPoints,
      oppPoints : oppPoints

team.addPlayer('Steph', 'Curry', 28);
team.addPlayer('Lisa', 'Leslie', 44);
team.addPlayer('Bugs', 'Bunny', 76);

team.addGame ('Titans', 100, 98);
team.addGame ('Workers', 103, 90);
team.addGame ('Sickers', 108, 78);;

There is no players property or getter, as such you get undefined (although an error might have been better, something like: no such property).

.push() is an array specific method. Push can not be applied to undefined.

1 Like

i considered this but still… i thought this would be solved if there is an let player variable… as it was suggested in the video tutorial…
also this code looks like the one created in the tutorial

Why would this solve the problem? A local variable within a method (scope rules) doesn’t in anyway affect properties of an object.

I strongly dislike when people say this. When working as a developer, there is no video to follow. You have to figure it out. Which you should do in this case

Start asking questions like: Where is the players property defined? Should it be defined, yes or no? What is the players properties purpose?

then start debugging from there.

ok then… thanks for the help… i guess

Programming is first and foremost problem solving. Sure i could tell you the answer, but that is missing the point.

Please work with me here. What are you trying to do and why?

i figured it out in the end, wrongly named the property getter
i guess the real problem was that , as a beginner, everything is very confusing when I try to understand the logic behind the code… hope this improves as time passes
thanks anyway

1 Like

If you have an object like so:

const stuff = {}

Then doing this:

const player = 5

Will not add a players property to your object. Right?

stuff.players  // nothing there

if you wanted such a property to exist … you’d have to put it there.

stuff.players = 3

if you attempt to look up some property, and it isn’t there, then you would next ask whether it should be there, and if so, where should it have been set, and then you’d go look at that, or, maybe it shouldn’t exist, and then you’d reconsider what to do, maybe you meant a different property.

Things only happen if you explicitly make it so.
And you can observe what you do as you do it by printing out involved values.

The programmer then very deliberately leverages actions to get what they want, and if something goes wrong they have to observe what they did and compare to what they meant.

And if there’s anything in your code that you don’t know what it does, then you have to find out, or remove it. If you don’t know what something does then you can’t use it to describe the actions you decided on.

1 Like

similar problem but I had called .addPlayer and not .addPlayer(s). if I can’t get things to work I save them in a work doc and the come back to them later in vs code. Its all starting to make sense now!

I have only been coding for about a month and I agree the debugging your own code is absolutely necessary for growth.

However I have had 2 programmers (one of which has been coding for 31 years) look over a few of the lessons I struggled a lot on (not this one) and both said the same thing" These instructions are poorly written".

Point being, everyone makes mistakes, even the developers who write some of the lessons. So maybe take it easy on us newbies who are not only trying to learn a mountain of new information but also doing it with (sometimes) poor instructions.

1 Like

I have a similar issue I am receiving an error on the same line:


TypeError: Cannot read property ‘push’ of undefined

I am not sure what I am doing wrong here. Please help!

Is it defined? Or should it be, this._players? Or perhaps the getter is not functioning as expected?

I figured it out.

I needed an ‘return’ inside the get players

get players() {

As well as the underscore before players.


1 Like