title: Doing a shortest job first algorithm in javascript
body: <p>I am trying to simulate shortest job first technique of os using javascript, given jobs/processes, arrival time and burst time:</p>
  
  <p>Checkout this link : <a href="http://guptapreeti.blogspot.com/2012/03/example-of-preemptive-sjf.html" rel="nofollow">Shortest Job First - Concept</a>. Now I have an array:</p>
  
  <pre><code>var arr = [
      {
          "job": "j4",
          "at": 0,
          "bt": 8
      },
      {
          "job": "j2",
          "at": 2,
          "bt": 4
      },
      {
          "job": "j3",
          "at": 2,
          "bt": 5
      },
      {
          "job": "j5",
          "at": 6,
          "bt": 4
      },
      {
          "job": "j1",
          "at": 8,
          "bt": 3
      }
  ];
  </code></pre>
  
  <p>And I wanted to create a new array containing objects of processes . .</p>
  
  <p>e.g</p>
  
  <pre><code>[
     {"job": "j4", "range" : "0-2"},
     {"job": "j2", "range" : "2-6"},
     {"job": "j5", "range" : "6-10"},
     {"job": "j1", "range" : "10-13"},
     {"job": "j3", "range" : "13-18"},
     {"job": "j4", "range" : "18-24"}
  ]
  </code></pre>
  
  <p>so I tried doing it, but I'm super stuck and nowhere near what I want to achieve.</p>
  
  <pre><code>for(i = 0; i &lt; arr.length; i++) {
     var temp = [];
      for(j = arr[i].at; j &lt; arr[i].bt; j++) {
          var clone = arr.slice(0);
          var arrived = clone.splice(0, i).filter(function( obj ) {
              return obj.at == j;
          });
          var shorter = arr[i];
          for(k = 0; k &lt; arrived.length; k++) {
              if(arrived[k].bt &lt; arr[i].bt) {
                  shorter = arrived[k];
                  arr[i].bt - (j - arr[i].at);
              }
          }
          if(shorter != arr[i]) {
              j = arr[i].bt;
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong>
      replaced the new array values with the real values if solved</p>
  
  <pre><code>Time Process
   0     j4(8)
   1 
   2     j4(6), j2`(4), j3(5)
   ...
   6     j4(6), j3(5), j5(4)         ::: j2 done
   7
   8     j5`(2), j4(6), j3(5), j1(3)
   9
   10                                ::: j5 done
   ...
   13                                ::: j1 done
   ...
   18                                ::: j3 done
   ...
   24                                ::: j4 done
  
  
  so the new array will be
  
  [
     {"job": "j4", "range" : "0-2"},
     {"job": "j2", "range" : "2-6"},
     {"job": "j5", "range" : "6-10"},
     {"job": "j1", "range" : "10-13"},
     {"job": "j3", "range" : "13-18"},
     {"job": "j4", "range" : "18-24"}
  ]
  </code></pre>