lundi 17 août 2020

I need help in assigning trailer compartments to animals

We have 4 compartments in a trailer:

var Compartments = [240, 240, 200, 180];

Now, We have to assign compartments to the animals for e.g.: Bulls are high in quantity so they have required 2 compartments (that I already have calculated by %)

var TotalLoadWeight = parseInt(bullWeight) + parseInt(cowWeight) + parseInt(calfWeight);
var bullperc = parseInt(bullWeight / TotalLoadWeight * 100);
var cowperc = parseInt(calfWeight / TotalLoadWeight * 100);
var calfperc = parseInt(calfWeight / TotalLoadWeight * 100);

var bullCompartment = Math.round(bullperc / 100 * 4); // 2
var cowCompartment = Math.round(cowperc / 100 * 4); // 1
var calfCompartment = Math.round(calfperc / 100 * 4); // 1

var compAllocate = [bullCompartment, cowCompartment, calfCompartment];

Problem Statement: Answer should be - Bull will take 240 & 240; Cow will take 200; Calf will take 180, but I am not getting this answer. More details: Assign first two compartments to Bull (because Bull count is 2) & those compartments will not repeat for next two.

for(var x = 0; x < compAllocate.length; x++{
    for(var i = 0; i < compAllocated[x]; i++){
        console.log("Compartments" + Compartments[x]); // Not getting proper answer.
    }
}

These loops are repeating: compartment 1 & 2 for bull, again compartment 1 for cow




Aucun commentaire:

Enregistrer un commentaire