Home > Enterprise >  Wondering why my last case in my switch statement is working properly
Wondering why my last case in my switch statement is working properly

Time:07-07

my switch statement works fine unless the match results in a tie and im not sure how to properly write the operation for it. I have tried several different approaches with no luck. At first I tried to use gameBoard.board[0]===("X"||"O") also with no luck. Any advice to pint me in the right direction will be very appreciated!

        switch(true){
            case gameBoard.board[0]==="X" && gameBoard.board[1]==="X" && gameBoard.board[2]==="X" :
                alert("Player1 Wins")
                break;
            case gameBoard.board[0]==="X" && gameBoard.board[3]==="X" && gameBoard.board[6]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[0]==="X" && gameBoard.board[4]==="X" && gameBoard.board[8]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[1]==="X" && gameBoard.board[4]==="X" && gameBoard.board[7]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[2]==="X" && gameBoard.board[5]==="X" && gameBoard.board[8]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[3]==="X" && gameBoard.board[4]==="X" && gameBoard.board[5]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[6]==="X" && gameBoard.board[7]==="X" && gameBoard.board[8]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[6]==="X" && gameBoard.board[4]==="X" && gameBoard.board[2]==="X":
                alert("Player1 Wins")
                break;
            case gameBoard.board[0]==="O" && gameBoard.board[1]==="O" && gameBoard.board[2]==="O" :
                alert("Player2 Wins")
                break;
            case gameBoard.board[0]==="O" && gameBoard.board[3]==="O" && gameBoard.board[6]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[0]==="O" && gameBoard.board[4]==="O" && gameBoard.board[8]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[1]==="O" && gameBoard.board[4]==="O" && gameBoard.board[7]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[2]==="O" && gameBoard.board[5]==="O" && gameBoard.board[8]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[3]==="O" && gameBoard.board[4]==="O" && gameBoard.board[5]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[6]==="O" && gameBoard.board[7]==="O" && gameBoard.board[8]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[6]==="O" && gameBoard.board[4]==="O" && gameBoard.board[2]==="O":
                alert("Player2 Wins")
                break;
            case gameBoard.board[0]===("X"||"O") && gameBoard.board[1]===("X"||"O") && gameBoard.board[2]===("X"||"O") && 
                 gameBoard.board[3]===("X"||"O") && gameBoard.board[4]===("X"||"O") && gameBoard.board[5]===("X"||"O") &&
                 gameBoard.board[6]===("X"||"O") && gameBoard.board[7]===("X"||"O") && gameBoard.board[8]===("X"||"O"):
            alert("You Tied, No WInner")
        }
        
    }
}

CodePudding user response:

This part
gameBoard.board[0]===("X"||"O") is not correct because you are evaluating the ("x" || "o") part, then comparing that with the gameBoard.board[0] value.
"X" || "O" will always return "X" because it is truthy

So you are basically comparing this for the entirety of the last case statement

gameBoard.board[0]==="X" && gameBoard.board[1]==="X" && gameBoard.board[2]==="X" .....(and more)

To fix it, it should be

(gameBoard.board[0]==="X" || gameBoard.board[0]==="O") && (gameBoard.board[1]==="X" || gameBoard.board[1]==="O") &&
(gameBoard.board[2]==="X" || gameBoard.board[2]==="O") ......(and more)

Or just use a loop to check it...

CodePudding user response:

The alert and break isn't indented so it won't execute. Also, when using conditions such as && or || you should use an if statement. Generally speaking, it's an incorrect use of the switch statement from my understanding. Either way, I hope this helps :)

  • Related