79342881

Date: 2025-01-09 14:02:48
Score: 2.5
Natty:
Report link

@gwcoffey - yep, that was the exact issue, thanks! Except I found a little bit of a cleaner way to solve the issue, which just involves changing the switchPlayers functionality like so:

document.querySelectorAll("button").forEach((button, index) => {
    button.addEventListener("click", () => {
      if (getBoard()[index] === "" && !checkForWinner()) {
        setBoard(getActivePlayer().symbol, index);
        button.textContent = getActivePlayer().symbol;
      }

      if (checkForWinner()) {
        console.log(`${getActivePlayer().name} has won!`);
        return;
      }

      switchPlayers();
    });
  });

where switchPlayers is just moved out of the conditional checks, and allows the switch to happen, and stops when the conditions are met. This seems to work from my testing, and I try to avoid nested conditionals in this regard, if possible. But thanks for answering...!

Reasons:
  • Blacklisted phrase (0.5): thanks
  • Blacklisted phrase (1): thanks for answering
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @gwcoffey
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Lushmoney