In the Tic Tak Toe game,
We have a function which checks for a winner, It should include a return false statement in the end.
This missing line of code is resulting in O being declared as a winner in case we have a draw.
URL of the file : https://github.com/shradha-khapra/JavaScriptSeries/tree/main/TicTacToe/app.js
Updated function with correct logic :
const checkWinner = () => {
for (let pattern of winPatterns) {
let pos1Val = boxes[pattern[0]].innerText;
let pos2Val = boxes[pattern[1]].innerText;
let pos3Val = boxes[pattern[2]].innerText;
if (pos1Val != "" && pos2Val != "" && pos3Val != "") {
if (pos1Val === pos2Val && pos2Val === pos3Val) {
showWinner(pos1Val);
return true;
}
}
}
return false;
};