title: Loop and count in JavaScript
body: <p>I try to get at least three responses from a total of 10 textboxes. And an alert will occur if the total number of responses is less than three. I also try to eliminate responses that are only spaces, so I use <code>return x.replace(/^\s+|\s+$/gm,'')</code> to get rid of spaces before and after the string.</p>
  <p>What I need to do is first set a variable &quot;count&quot; to be 0. Next to detect whether there is at least one character in a specific textbox, and if so, add 1 to &quot;count&quot;. Then do this for all 10 textboxes. After that, check whether &quot;count&quot; is greater than 2; if so, go to a next page; otherwise, an alert will occur.</p>
  <p>But it didn't work.</p>
  <p>Another question is, how can I avoid iterating the process for all 10 textboxes?</p>
  <pre><code>var count=0
  
   function myTrim(x) {
    return x.replace(/^\s+|\s+$/gm,'');
  }
  
  var x1 =  document.getElementById(&quot;textbox1&quot;).value; 
    var t1 = myTrim(&quot;x1&quot;);
    var n1 = t1.length;
  
    if  (n1&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  
  var x2 =  document.getElementById(&quot;textbox2&quot;).value; 
    var t2 = myTrim(&quot;x2&quot;);
    var n2 = t2.length;
  
    if  (n2&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  
  var x3 =  document.getElementById(&quot;textbox3&quot;).value; 
    var t3 = myTrim(&quot;x3&quot;);
    var n3 = t3.length;
  
    if  (n3&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  var x4 =  document.getElementById(&quot;textbox4&quot;).value; 
    var t4 = myTrim(&quot;x4&quot;);
    var n4 = t4.length;
  
    if  (n4&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  
  var x5 =  document.getElementById(&quot;textbox5&quot;).value; 
    var t5 = myTrim(&quot;x5&quot;);
    var n5 = t5.length;
  
    if  (n5&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  var x6 =  document.getElementById(&quot;textbox6&quot;).value; 
    var t6 = myTrim(&quot;x6&quot;);
    var n6 = t6.length;
  
    if  (n6&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  var x7 =  document.getElementById(&quot;textbox7&quot;).value; 
    var t7 = myTrim(&quot;x7&quot;);
    var n7 = t7.length;
  
    if  (n7&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  
  var x8 =  document.getElementById(&quot;textbox8&quot;).value; 
    var t8 = myTrim(&quot;x8&quot;);
    var n8 = t8.length;
  
    if  (n8&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  var x9 =  document.getElementById(&quot;textbox9&quot;).value; 
    var t9 = myTrim(&quot;x9&quot;);
    var n9 = t9.length;
  
    if  (n9&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  var x10 =  document.getElementById(&quot;textbox10&quot;).value; 
    var t10 = myTrim(&quot;x10&quot;);
    var n10 = t10.length;
  
    if  (n10&lt;1){
      count=count+0
    } else{
      count=count+1
    }
  
  
  
  function checknumber() {
      if (count &lt; 3){
              alert(&quot;You must enter at least 3 responses before you can continue.&quot;);
          } else {
              location.href=&quot;https://www.kindpng.com/imgv/hxbmxi_symbol-thumbs-up-clip-art-vector-free-clipart/&quot; ; // if selection made, go to next page
          }
      }```
  </code></pre>