title: How to change the order of jobs placing in the Machines - javascript
body: <p>I am newbie to javascript.I am doing a program sequencing number of jobs in n number of machine.I found this code logic online.But i have a small problem.That highest time consuming job goes to last machines.I want highest time consuming job should place on the first machine..
  this is my ouput</p>
  
  <pre><code>8 Jobs Inserted
  JobId:j8 Time to finish this Job :22
  JobId:j7 Time to finish this Job :11
  JobId:j3 Time to finish this Job :10
  JobId:j2 Time to finish this Job :8
  JobId:j4 Time to finish this Job :5
  JobId:j5 Time to finish this Job :4
  JobId:j1 Time to finish this Job :2
  JobId:j6 Time to finish this Job :1
  Machines Available :5
  Output =====================================&gt;
  ___________________________________________________________________________________________________________________
  MachineId:M1 Jobs done by this Machine:(j4 = 5)(j5 = 9)(j6 = 10) Time consumed to finish all jobs:10
  ___________________________________________________________________________________________________________________
  MachineId:M2 Jobs done by this Machine:(j2 = 8)(j1 = 10) Time consumed to finish all jobs:10
  ___________________________________________________________________________________________________________________
  MachineId:M3 Jobs done by this Machine:(j3 = 10) Time consumed to finish all jobs:10
  ___________________________________________________________________________________________________________________
  MachineId:M4 Jobs done by this Machine:(j7 = 11) Time consumed to finish all jobs:11
  ___________________________________________________________________________________________________________________
  MachineId:M5 Jobs done by this Machine:(j8 = 22) Time consumed to finish all jobs:22
  ______________________________________________
  Highest and Lowest Time consuming Machine =========&gt;
  M5 is the highest running Machine 22
  M1 is the Lowest running Machine 10
  M2 is the Lowest running Machine 10
  M3 is the Lowest running Machine 10
  </code></pre>
  
  <p>code.js</p>
  
  <pre><code>var _ = require('underscore');
  
  var njobs = [2,8,10,5,4,1,11,22];
  var howManyMachines = 5;
  
  console.log(njobs.length+" Jobs Inserted")
  
  var jobswithvalue = Array(njobs.length).fill().map((m,i) =&gt; {
     //To remove Array Value 0
          var Jindex = i;
          if(Jindex == 0){
              Jindex = 1
          }else{
              Jindex = i + 1
          }
  
     return {
         jobname:"j"+Jindex,
         time:njobs[i]
     }
  });
  
  
  
  var sorted = jobswithvalue.sort((a,b) =&gt; b.time -a.time );
  
  
  var machines = Array(howManyMachines).fill().map((m, i) =&gt; {
      var Mindex = i;
      if(i == 0){
         Mindex = 1
      }else{
          Mindex = i+1
      }
      return {
          id: i,
          value: 0,
          jobs: [],
          name:"M"+Mindex
  
      } });
  
  
  sorted.map(function(data) {
      console.log('JobId:' + data.jobname + ' Time to finish this Job :' + data.time)
  });
  
  
  
  console.log('Machines Available :' + machines.length);
  //Loop it and assign it
  sorted.forEach(job =&gt; {
      var minMachine = machines
          .slice(1)
          .reduce((res, cur) =&gt;
              res.value &lt; cur.value ? res : cur, machines[0]);
  
      minMachine.jobs.push(job);
      minMachine.value += job.time;
  });
  
  
  //Prints the value
  
  console.log('Output =====================================&gt;');
  
  
  
  machines.map(function(data) {
      var str = "MachineId:" + data.name + " Jobs done by this Machine:"
      data.jobs.reduce(function(total, curr) {
          var newTotal = total + curr.time;
          str += "(" + curr.jobname + " = " + newTotal + ")";
          return total + curr.time;
      }, 0);
      str += ' Time consumed to finish all jobs:' + data.value;
      console.log('___________________________________________________________________________________________________________________');
      console.log(str);
  });
  
  
  
  var high = Math.max.apply(Math, machines.map(function(o) {
      return o.value;
  }));
  console.log('______________________________________________')
  console.log('Highest and Lowest Time consuming Machine =========&gt;');
  
  var resultHigh = _.filter(machines,function (item) {
      return item.value == high
  });
  
  
  resultHigh.map(function (data) {
  
      console.log(data.name+' is the highest running Machine '+data.value)
  });
  
  var low = Math.min.apply(Math, machines.map(function(o) {
      return o.value;
  }));
  var resultLow = _.filter(machines,function (item) {
      return item.value == low
  });
  resultLow.map(function (data) {
  
      console.log(data.name+' is the Lowest running Machine '+data.value)
  });
  </code></pre>
  
  <p>this the code to modify</p>
  
  <pre><code>//Loop it and assign it
  sorted.forEach(job =&gt; {
      var minMachine = machines
          .slice(1)
          .reduce((res, cur) =&gt;
              res.value &lt; cur.value ? res : cur, machines[0]);
  
      minMachine.jobs.push(job);
      minMachine.value += job.time;
  });
  </code></pre>