I am writing a functioning Tic Tac Toe program and I am pretty much done. Except my win condition is too long and ugly. It looks like this.
function checkWin(){
if(board[0].textContent === "X" &&
board[1].textContent === "X" &&
board[2].textContent === "X"
) { alert("Win")}
else if (
board[3].textContent === "X" &&
board[4].textContent === "X" &&
board[5].textContent === "X"
) { alert("Win")}
else if (
board[6].textContent === "X" &&
board[7].textContent === "X" &&
board[8].textContent === "X"
) { alert("Win")}
}
I only write a few win condition because if I write the whole thing it will be even longer. I was wondering how I can write a shorter version. I was thinking of doing an array and looping through it but I can't figure out how. It will be something like this.
var winConditions = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [6,4,2]]
but how can I use checkWin to loop through winConditions and make it equal to X and O?