wrarar wrarar - 1 month ago 6
Javascript Question

How to convert my output to Matrices in Javascript

I am newbie to javascript.I pretty confused that how to convert my output to matix formation like this..

From this output to

Output

MachineId:M1 Jobs done By this Machine:(j5 = 8),(j4 = 7),(j3 = 1) Time consumed to finish all jobs:16
MachineId:M2 Jobs done By this Machine:(j2 = 9),(j6 = 5),(j1 = 4) Time consumed to finish all jobs:18
MachineId:M3 Jobs done By this Machine:(j7 = 22) Time consumed to finish all jobs:22
MachineId:M4 Jobs done By this Machine:(j8 = 32) Time consumed to finish all jobs:32


This output should be printed

[8,7,1][16],
[9,5,4][18],
[22,0,0][22],
[32,0,0][32]


This is my Actual code produce the first mentioned output

// real algorithm
//total Jobs
var _ = require('underscore');
var njobs = [{
jobname: "j1",
time: 4
}, {
jobname: "j2",
time: 9
}, {
jobname: "j3",
time: 1
}, {
jobname: "j4",
time: 7
}, {
jobname: "j5",
time: 8
}, {
jobname: "j6",
time: 5
}, {
jobname: "j7",
time: 22
},
{
jobname: "j8",
time: 32
}

];

//sort this data with time
var sorted = njobs.sort((a, b) => b.time - a.time);

console.log('Jobs Inserted ====================>')

sorted.map(function (data) {
console.log('JobId:'+data.jobname+' Time to finish this Job :'+data.time)
});

//List all the machines
var machines = [
{
id:1,
value:0,
jobs:[],
name:'M1'
},
{
id:2,
value:0,
jobs:[],
name:'M2'
},
{
id:3,
value:0,
jobs:[],
name:'M3'
},
{
id:3,
value:0,
jobs:[],
name:'M4'
}
];
console.log('Machines Available :'+ machines.length);
//Loop it and assign it
sorted.forEach(job => {
var minMachine = machines
.slice(1)
.reduce((res, cur) =>
res.value < cur.value ? res : cur, machines[0]);

minMachine.jobs.push(job);
minMachine.value += job.time;
});


//Prints the value

console.log('Output =====================================>');



machines.map(function (data) {
console.log('MachineId:'+data.name+' Jobs done By this Machine:'+data.jobs.map(data => '('+data.jobname+' = '+data.time+')')+' Time consumed to finish all jobs:'+data.value)
});



var high = Math.max.apply(Math,machines.map(function (o) {
return o.value;
}));
console.log('______________________________________________')
console.log('Highest Time consuming Machine =========>');
var result = _.findWhere(machines,{value:high});
console.log(result.name+' is the highest running Machine '+result.value);

// machines.map(function (){...}).reduce(function (total, curr) { return total + curr; });

mab mab
Answer

Add this to the bottom of your JavaScript.

// Build the multidimensional array.
var arr = [];
var maxJobsPerMachine = 0;
machines.map(function (data) {
    var marr = [];
    marr.push(data.jobs.map(data => data.time));
    maxJobsPerMachine=Math.max(maxJobsPerMachine, marr[0].length);
    marr.push([data.value])
    arr.push(marr);
});
arr.forEach(e => { while (e[0].length < maxJobsPerMachine) e[0].push(0); });

// Print it.
arr.forEach(e => { 
    var line = ""; 
    e.forEach(f => line += "[" + f.join(",") + "]");
    console.log(line);
});

Here is the output:

[8,7,1][16]
[9,5,4][18]
[22,0,0][22]
[32,0,0][32]