title: Inserting a loop into a Javascript number guessing game
body: <p>For an assignment, I need to make a JS number guessing game. It needs to include a loop to check the user's guess and a reset game button. My problem is getting the loop to work. I want the number of turns to start at 10. Each time the user makes an incorrect guess, their number of turns decreases by 1, and if they guess correctly, their number of turns is 0. If they push the "Start New Game" button, a new number should be generated and the number of turns should be reset to 10.</p>
  
  <p>The loop doesn't specifically need to be a while loop, I just need one in the code for my assignment. Can anybody help me out?</p>
  
  <pre><code>&lt;body&gt;
      &lt;!-- GAME INSTRUCTIONS --&gt;
      &lt;h1&gt;Number Guessing Game&lt;/h1&gt;
      &lt;p&gt;A random number between 1 and 100 has been generated. Can you guess it?&lt;/p&gt;
      &lt;!-- FORM (Includes button to confirm guess, input box, and output box) --&gt;
      &lt;form id="Input" name="Input"&gt;
          &lt;input name="guess" placeholder="Insert your guess" type="number"&gt;
          &lt;input name="requestInfo" onclick="getResults()" type="button" value="Confirm"&gt;
          &lt;p&gt;&lt;/p&gt;
          &lt;textarea cols="50" name="results" readonly="true" rows="8"&gt;&lt;/textarea&gt;
          &lt;p&gt;&lt;/p&gt;&lt;input name="newGame" onclick="resetGame()" type="button" value="Start New Game"&gt;
      &lt;/form&gt;&lt;!-- JAVASCRIPT START --&gt;
      &lt;script type="text/javascript"&gt;
  
  // Define variables
  var num = Math.floor(Math.random() * 100) + 1;
  var turns = 10;
  
  function checkNumber() {
      var guess = parseFloat(document.Input.guess.value);
      while (turns &gt; 0) {
              if (guess == num) {
                  turns = 0;
                  document.Input.results.value = "Congratulations, you won! The mystery number was " + num + ".";
              } else if (guess &lt; num) {
                  turns--;
                  document.Input.results.value = "Your guess was too low. Turns remaining: " + turns;
              } else if (guess &gt; num) {
                  turns--;
                  document.Input.results.value = "Your guess was too high. Turns remaining: " + turns;
              }
      }
  }
  
  function resetGame() {
      turns = 10;
      num = Math.floor(Math.random() * 100) + 1;
      document.Input.guess.value = "";
      document.Input.results.value = "";
  }
  
  function getResults() {
      checkNumber();
  }
      &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>