title: JavaScript For Loop to Repeat Code
body: <p>Sorry for the vague question title - unsure how to word this.</p>
  
  <p>Basically, I have the following JavaScript that I want to condense using a for loop.</p>
  
  <pre><code>$('.q1').keyup(function () {
      if ($.inArray($(this).val().toLowerCase(), a1) &gt; -1) {
          //Stuff
      } else {
          //Other stuff
      };
  });
  
  $('.q2').keyup(function () {
      if ($.inArray($(this).val().toLowerCase(), a2) &gt; -1) {
          //Stuff
      } else {
          //Other stuff
      };
  });
  </code></pre>
  
  <p>You can see that the only change between these two functions is the first class (either .q1 or .q2) and the array name (either a1 or a2).
  So far, I have done the following...</p>
  
  <pre><code>for (var i=1; i&lt;=8; i++) {
      $('.q'+i).keyup(function () {
          if ($.inArray($(this).val().toLowerCase(), a1) &gt; -1) {
              //Stuff
          } else {
              //Other stuff
          };
      });
  }
  </code></pre>
  
  <p>You can see that the first class issue has been solved with the incrementing i variable, but I am unsure how I can make the array value change on each loop so that it goes a1, a2, a3, a4 etc.</p>
  
  <p>Any help would be much appreciated. Thank you.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The a1 and a2 refers to arrays...</p>
  
  <pre><code>var a1 = new Array();
  a1[0] = "egypt";
  
  var a2 = new Array();
  a2[0] = "brasil";
  a2[1] = "brazil";
  </code></pre>
  
  <p>I have an array for each answer to a quiz. In each array are the possible answers to each question.</p>