title: Round Robin Scheduling Algorithm in JavaScript
body: <p>i implemented RR scheduling algorithm with arrival time in javascript. but it have some issues. it is not give the right answer. process queue show correctly  </p>
  
  <pre><code>    function roundrobin(p,a,b,n){
          var seq=[]
          var res_b=[];
          var res_a=[];
          for (var x = 0; x &lt;b.length; x++) {
                  res_b[x] = b[x]; 
                  res_a[x] = a[x]; 
              }    
          var t=0;
          var w=[];
          var comp=[];
          while(true){
              var flag=true;
              for(var i=0;i&lt;p.length;i++){
                  if (res_a[i]&lt;=t){
                      if(res_a[i]&lt;=n){
                          if(res_b[i]&gt;0){
                              flag=false;
                              if (res_b[i] &gt; n) { 
  
                                  t = t + n; 
                                  res_b[i] = res_b[i] - n; 
                                  res_a[i] = res_a[i] + n; 
                                  seq.push(p[i]); 
                              } 
                              else { 
                                  t = t + res_b[i];  
                                  comp[i] = t - a[i];  
                                  w[i] = t - b[i] - a[i]; 
                                  res_b[i] = 0; 
                                  seq.push(p[i]);
                              }
                          }
                      }
                      else if (res_a[i] &gt; n) { 
                          for (var j = 0; j &lt; p.length; j++) { 
  
                              if (res_a[j] &lt; res_a[i]) { 
                                  if (res_b[j] &gt; 0) { 
                                      flag = false; 
                                      if (res_b[j] &gt; n) { 
                                          t = t + n; 
                                          res_b[j] = res_b[j] - n; 
                                          res_a[j] = res_a[j] + n; 
                                          seq.push(p[j]); 
                                      } 
                                      else { 
                                          t = t + res_b[j]; 
                                          comp[j] = t - a[j]; 
                                          w[j] = t - b[j] - a[j]; 
                                          res_b[j] = 0; 
                                          seq.push(p[j]); 
                                      } 
                                  } 
                              } 
                          } 
  
                          if (res_b[i] &gt; 0) { 
                              flag = false;  
                              if (res_b[i] &gt; n) { 
                                  t = t + n; 
                                  res_b[i] = res_b[i] - n; 
                                  res_a[i] = res_a[i] + n; 
                                  seq.push(p[i]); 
                              } 
                              else { 
                                  t = t + res_b[i]; 
                                  comp[i] = t - a[i]; 
                                  w[i] = t - b[i] - a[i]; 
                                  res_b[i] = 0; 
                                  seq.push(p[i]);
                              } 
                          } 
                      } 
                  }
                  else if (res_a[i] &gt; t) { 
                      t++; 
                      i--; 
  
  
              }
          }
          if (flag) { 
              break; 
          } 
  
          }
          document.getElementById("demo1").innerHTML = seq;
          document.getElementById("demo2").innerHTML = w;
          document.getElementById("demo3").innerHTML = comp;
      }
  </code></pre>
  
  <p>give me some suggestion to the right implementation. (i worked on this code many days)
  Round robin  is the  scheduling algorithm used by the CPU during execution of the process . Round robin is designed specifically for time sharing systems . It is similar to first come first serve scheduling algorithm but the preemption  is the added functionality to switch between the processes </p>