Team Stats

Course LINK

Hi guys!
So I was trying to add a little bit to my Team Stats project. Basically, I wanted to create a method that determines wins, draws and losses using a for loop or a foreach loop. I tried the foreach loop first and it gave an error saying ‘not a function’. The for loop I have (see included code) does something but returns that all results are draws which they are not. PLEASE HELP!
Thanks in advance :slight_smile:

calculateStats() {
        let wins = 0
        let draws = 0
        let losses = 0
        for(let i = 0; i < this.games.length; i++) {
            if (this.games[i].teamPoints > this.games[i].opponentPoints){
                wins++
            } else if (this.games[i].teamPoints === this.games[i].opponentPoints){
                draws++
            } else {
                losses++
            };
        };
        console.log(
            `Wins: ${wins}
            Draws: ${draws}
            Losses: ${losses}`
        )
    }
};
1 Like

Your code is spot on! It works like a charm to me :wink: .

Are you sure you put the method inside the object and using proper indentation?
Could you post all your code inside a code block so I can look into it?

1 Like

Is there such a thing in JS? The only purpose of indentation is readability, and there is no proper way to indent considering it has no effect on how the code is interpreted.

2 Likes

Something to consider…

  stats () {
    let games = this.games;
    let wins, losses, ties;
    wins = 0;
    losses = 0;
    ties = 0;
    games.forEach(x => {
      x.teamPoints > x.opponentPoints ? wins++ :
      x.teamPoints < x.opponentPoints ? losses++ : ties++;
    });
    console.log(`Wins: ${wins}
Losses: ${losses}
Ties: ${ties}`
    )
  }

True but it will help in debugging. I added the code to my object and changed nothing to it, yet it worked like a charm.