Tic Tac Toe javascript. How to store player wins and to alert if there is a tie?
<p>I am currently creating a javascript tic tac toe game. I manage to get to display which player won. Somehow I currently struggle how to display a message when the game ends with a draw. </p>

<p>One more thing is how do I create a scoreboard and alert it when one of the player has won the game three times.</p>

<p>here is my code.</p>

<pre><code>&lt;html&gt;
&lt;title&gt;Tic Tac Toe&lt;/title&gt;
&lt;head&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/style.css" /&gt;
  &lt;script type="text/javascript" src="js/app.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="startGame();"&gt;
  &lt;h1&gt;Tic Tac Toe&lt;/h1&gt;
  &lt;h2&gt;By Irsyad&lt;/h2&gt;
  &lt;div id ="message"&gt;&lt;h3&gt; messages will go here&lt;h3&gt;&lt;/div&gt;
  &lt;div&gt;
    &lt;a href="javascript:startGame();"&gt;Restart&lt;/a&gt;
  &lt;/div&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td id="s1" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
      &lt;td id="s2" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
      &lt;td id="s3" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td id="s4" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
      &lt;td id="s5" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
      &lt;td id="s6" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td id="s7" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
      &lt;td id="s8" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
      &lt;td id="s9" class="square" onclick="nextMove(this);"&gt;&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>and this is the javascript.</p>

<pre><code>function startGame () {

for (var i = 1; i &lt;= 9; i++) {
     clearBox(i);
}
  document.turn = "X" ;
  if (Math.random() &lt; 0.5) {
    document.turn = "O" ;
  }
  document.winner = null;
  setMessage(document.turn + " gets to start");
}

function setMessage(msg) {
  document.getElementById('message').innerText = msg;
}

function nextMove(square) {
  if (document.winner != null) {
    setMessage(document.winner + " already won the game");
  }
  else if (square.innerText == ""){
    square.innerText = document.turn;
    switchTurn();
  } else {
    setMessage("That square is already used");
  }
}

function switchTurn() {
   if (checkForWinner(document.turn)) {
     setMessage("Congratulations, " + document.turn + " ! You win!");
     document.winner = document.turn;
   }
   else if (document.turn == "X") {
       document.turn = "O";
       setMessage("It's " + document.turn + " 's turn")

   } else {
      document.turn = "X";
      setMessage("It's " + document.turn + " 's turn")

   }
}

function checkForWinner(move) {
  var result= false;
  if (checkRow(1, 2, 3, move) ||
      checkRow(4, 5, 6, move) ||
      checkRow(7, 8, 9, move) ||
      checkRow(1, 4, 7, move) ||
      checkRow(2, 5, 8, move) ||
      checkRow(3, 6, 9, move) ||
      checkRow(1, 5, 9, move) ||
      checkRow(3, 5, 7, move)) {

      result = true;
      return result;
      }

}

function checkRow(a, b, c, move) {
   var result = false;
   if (getBox(a) == move &amp;&amp; getBox(b) == move &amp;&amp; getBox(c) == move){
     result = true;
   }
   return result;
}

function getBox(number) {
   return document.getElementById("s" + number).innerText;
}

function clearBox(number) {
  document.getElementById("s" + number).innerText = "";
}
</code></pre>