title: JS: This algorithm is doing weird thing, (analysis)
body: <p>This code is took from my constructor a tiny part. </p>
  
  <p>The first output should be ('1' or '2') then '0' til the end; Instead of pure '0' only. It gives me 1000 of 0. I don't know why. </p>
  
  <p>I tested with lower values at the 'cols' and 'row' variables, and it works fine. (it outputs: '1' the first time, then '0')But when the cols and rows are higher the problem appears again.
  Its for a memory limit? I dont know about how much memory.</p>
  
  <p>What should I do, to avoid this problem in future? Should I use functions? for each or something like recursive? Thank you.</p>
  
  <p>P.N. if you dont seem any problem, try to set higher values to cols and rows. maybe this should trigger the issue.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var i, row_rate
  var cols = 50
  var rows = 31
  var count = 0
  
  for (i = 0; i &lt; cols * rows; i++) {
  
    row_rate = Math.trunc(Math.trunc(i / cols) * 100 / rows)
  
    if (count &gt; 0) {
      console.log(0)
    }
    else if (row_rate &lt;= 20) {
      // the first time must pass here...
      count++
      console.log(1)
    }
    else {
      // ...or here
      count++
      console.log(2)
    }
  }</code></pre>
  </div>
  </div>
  </p>