0

I have made the game Connect four. When i use an alert to show the winner, the alert box pops up before the last piece is placed. Therefore i would like to use the DOM to announce the winner.

I have made a <div id="winPlayer1">Red Wins!</div> in HTML

And my JS looks like this:

function gameOver() {

    if (activePlayer == 0) {
        document.getElementById("winPlayer1").innerHTML;
    } else {
        pdocument.getElementById("winPlayer2").innerHTML;
    }
    gameActive = false;
}

How do i get the message to pop up when there is a winner?

Now they are both shown on the page even when there is no winner.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
A.Dyr
  • 3
  • 5
  • This isn't a duplicate of that question. The problem here is 1. `document.getElementById("winPlayer1").innerHTML;` does absolutely nothing and 2. there's a typo `pdocument` which will cause an error. –  Nov 14 '18 at 09:23
  • Thanks for spotting the typo Chris. What am i missing for this to work ? – A.Dyr Nov 14 '18 at 09:45
  • 1
    Here's your code as you intended it I guess (display one of two divs): https://jsfiddle.net/khrismuc/od4x1037/ Here's another version (set text of single div): https://jsfiddle.net/khrismuc/bjy754cp/ –  Nov 14 '18 at 09:54
  • Thanks a lot! @ChrisG – A.Dyr Nov 14 '18 at 10:28

0 Answers0