vendredi 30 octobre 2020

Why wont the if-statement in my function run?

Ive searched all over this site for similar questions and haven't been able to find an answer to help me. With that being said, sorry if this was asked before and I missed it.

After the if statement (winner != null) fires and the gameWon() is called, the console.log works but then nothing happens with the if-statement in gameWon(). But if I call gameWon() myself in the console then the whole function runs properly.

Any idea why it works when I call it manually but not when checkWinner() calls it?

{
  checkWinner: function () {
    //check for left to right diaganol
    if (this.equalCheck(boardArr[0], boardArr[4], boardArr[8]))
      winner = boardArr[0];
    //check for right to left diagonal
    if (this.equalCheck(boardArr[6], boardArr[4], boardArr[2]))
      winner = boardArr[6];
    //check for vertical win
    if (this.equalCheck(boardArr[0], boardArr[3], boardArr[6]))
      winner = boardArr[0];
    else if (this.equalCheck(boardArr[1], boardArr[4], boardArr[7])) {
      winner = boardArr[1];
    } else if (this.equalCheck(boardArr[2], boardArr[5], boardArr[8])) {
      winner = boardArr[2];
    }
    //check for horizontal win
    if (this.equalCheck(boardArr[0], boardArr[1], boardArr[2]))
      winner = boardArr[0];
    else if (this.equalCheck(boardArr[3], boardArr[4], boardArr[5])) {
      winner = boardArr[3];
    } else if (this.equalCheck(boardArr[6], boardArr[7], boardArr[8])) {
      winner = boardArr[6];
    }
    if (winner != null) {
      this.gameWon(winner);
    }
  },

  gameWon: function () {
    console.log("WORKS");
    if (winner == "X") {
      this.scoreDiv.innerText = `${player1.name} won!!`;
    } else if (winner == "O") {
      this.scoreDiv.innerText = `${player2.name} won!!`;
    } else return;
  },
}

Aucun commentaire:

Enregistrer un commentaire