title: Javascript alert box shows up before executing previous statement
body: <p>I am having a strange issue, but it is not surprising as I am a bit of a JavaScript newbie. Basically I am creating a simple high-low card game. (Draw two cards, highest card wins). Anyways, the code is below.</p>
  <p>The basic flow of the program is pretty simple. I choose 2 random numbers (1-52). These numbers are mapped to a corresponding card. (i.e. number 1 is the ace of spades, number 37 is the jack of clubs, etc.). Anyways, after drawing the cards, the program is to display the corresponding card and determine the winner. At the end of all of this, i have an alert that comes up and and tells the winner of the draw and asks if the user wants to play again.</p>
  <p>The problem I am having is this: Even though the program should have already displayed the image of the card and output the results to a text area, the alert box shows up before any of that actually occurs and never displays the cards or the results. Any ideas? I am posting all of the code so far and any help would be appreciated. Thanks in advance.</p>
  <pre><code>function drawCards() {
      var oppCard = randNumber();
      var customerCard = randNumber();
  
      while (oppCard == customerCard) {
          customerCard = randNumber();
      }
      var oppCardName = displayCard(oppCard, &quot;oppImage&quot;);
      var customerCardName = displayCard(customerCard, &quot;custImage&quot;);
  
      var result2 = &quot;Your card was: &quot; + customerCardName;
      var result1 = &quot;The opponent's card was: &quot; + oppCardName;
  
      var result3 = determineWinner(oppCard, customerCard);
      var result4 = result3 + '\n' + result1 + '\n' + result2;
      $(&quot;#textareaRes&quot;).text(result4);
  
      playAgain(result3);
  }
  
  function determineWinner(oppsCard, customersCard) {
      var oppValue = oppsCard % 13;
      var customerValue = oppsCard % 13;
  
      var winnerString = &quot;&quot;;
      if (oppValue == 0) {
          oppValue = 13;
      }
      if (customerValue == 0) {
          customerValue = 13;
      }
      if (oppValue == customerValue) {
          winnerString = &quot;You Tied.&quot;;
      }
      else if (oppValue &gt; customerValue) {
          winnerString = &quot;You Lose.&quot;;
      }
      else if (oppValue &lt; customerValue) {
          winnerString = &quot;You Win!!&quot;;
      }
      return winnerString;
  }
  
  function randNumber() {
      var min = 1;
      var max = 52;
      var random = Math.floor(Math.random() * (max - min + 1)) + min;
      return random;
  }
  
  function playAgain(resultString) {
      if (resultString == &quot;You Lose.&quot; || resultString == &quot;You Win!!&quot;) {
          alert(resultString);
          var conf = confirm(&quot;Play Again?&quot;);
          if (conf == true) {
              $(&quot;#textareaRes&quot;).text(&quot;&quot;);
              document.getElementById(&quot;custImage&quot;).src=&quot;./cardImages/default.png&quot;;
              document.getElementById(&quot;oppImage&quot;).src=&quot;./cardImages/default.png&quot;;
          }
          else {
              window.location = &quot;#mainMenuPage&quot;;
          }
      }
      else {
          alert(resultString);
          alert(&quot;Try Again.&quot;);
          $(&quot;#textareaRes&quot;).text(&quot;&quot;);
          document.getElementById(&quot;custImage&quot;).src=&quot;./cardImages/default.png&quot;;
          document.getElementById(&quot;oppImage&quot;).src=&quot;./cardImages/default.png&quot;;
      }
  }
  </code></pre>
  <p>So I did not place the code in here for the display card function, just because for testing it is exceptionally long. It is just a giant switch case for all 52 random numbers. The finished product will actually be pulling from an XML file, but I used this just for testing purposes. (If, for some reason, you need to see the display cards function, let me know and I can post it.) Anyway, to recap, the last call made in the drawCards() function is the playAgain function. Upon running this code the results nor the card images are displayed. It just jumps straight to the alert that is called for by the playAgain function. This is probably a pretty noobish question, but I am a little perplexed by it. So any help you guys can offer would be GREATLY appreciated. Thanks.</p>
  <p>EDIT: It actually performs correctly in a computer's browser. However, the problem happens on a mobile device like a phone or tablet. So this is probably something that I am doing incorrectly here. Any help is greatly appreciated.</p>