title: Stop score going negative and over 52
body: <p>I have a text based war game where the teams start with a score of 26 (userScore and computerScore).  When a player wins, it goes up and when a player loses, it goes down. </p>
  
  <p>Problem is, the score goes negative and goes above 52 (in the event of a war at 51 cards), which would be all of the cards plus some (can't have more than 52 cards).</p>
  
  <p>How can I limit userScore and computerScore from going negative or over 52?</p>
  
  <pre><code>var suits = ["Spades", "Hearts", "Clubs", "Diamonds"];
  var cards = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"];
  
  var attempts = 1;
  var war = false;
  
  var computerScore = 26;
  var userScore = 26;
  
  
  while (computerScore &lt; 52 &amp;&amp; userScore &lt; 52)
  {
  
    var computerIndex = Math.floor(Math.random() * cards.length);
    var userIndex = Math.floor(Math.random() * cards.length);
  
    var computerCard = cards[computerIndex];
    var userCard = cards[userIndex];
    var computerSuit = suits[Math.floor(Math.random()*suits.length)];
    var userSuit = suits[Math.floor(Math.random()*suits.length)];
  
    alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
  
    if (computerIndex &gt; userIndex &amp;&amp; war == false)
    {
      computerScore++;
      userScore--;
      alert("I win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
    }
  
    else if (computerIndex &lt; userIndex &amp;&amp; war == false)
    {
      computerScore--;
      userScore++;
      alert("You win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
    }
  
    else if (computerIndex == userIndex &amp;&amp; war == false)
    {
      alert("TIE! TIME FOR WAR! \n\nI have " + computerScore + " cards and you have " + userScore + " cards")
      war = true;
  
      var computerIndex = Math.floor(Math.random() * cards.length);
      var userIndex = Math.floor(Math.random() * cards.length);
  
      var computerCard = cards[computerIndex];
      var userCard = cards[userIndex];
      var computerSuit = suits[Math.floor(Math.random()*suits.length)];
      var userSuit = suits[Math.floor(Math.random()*suits.length)];
  
      alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
  
        if (computerIndex &gt; userIndex &amp;&amp; war == true)
            { 
              computerScore = computerScore + (attempts * 3);
              userScore = userScore - (attempts * 3);
              alert("I win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
              war = false;
              attempts = 1;
            }
  
            else if (computerIndex &lt; userIndex &amp;&amp; war == true)
            {
              userScore = userScore + (attempts * 3);
              computerScore = computerScore - (attempts * 3);
              alert("You win. \n\nI have " + computerScore + " cards and you have " + userScore + " cards.");
              war = false;
              attempts = 1;
            }
  
            else 
            {
              alert("TIE! TIME FOR WAR (AGAIN)! \n\nI have " + computerScore + " cards and you have " + userScore + " cards")
              attempts++;
  
              var computerIndex = Math.floor(Math.random() * cards.length);
              var userIndex = Math.floor(Math.random() * cards.length);
  
              var computerCard = cards[computerIndex];
              var userCard = cards[userIndex];
              var computerSuit = suits[Math.floor(Math.random()*suits.length)];
              var userSuit = suits[Math.floor(Math.random()*suits.length)];
  
              alert("I drew a " + computerCard + " of " + computerSuit +"\nYou drew a " + userCard + " of " + userSuit);
  
              if (computerIndex == userIndex)
              {
                war = true;
              }
  
              else
              {
                war = false;
              }
            }
    }
  }  
  
                if (computerScore &gt;= 52)
                  {
                    alert("I WIN!  GOOD GAME!");
                  }
                    else
                  {
                    alert("YOU WIN!  GOOD GAME!")
                  }
  </code></pre>