首页 > 解决方案 > 我需要帮助将拖车隔间分配给动物

问题描述

我们的拖车有 4 个隔间:

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

现在,我们必须为动物分配隔间,例如:公牛数量众多,因此它们需要 2 个隔间(我已经按百分比计算)

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];

问题陈述:答案应该是 - Bull will take 240 & 240; 牛要200;小牛需要 180,但我没有得到这个答案。更多详细信息:将前两个隔间分配给 Bull(因为 Bull 计数为 2)并且这些隔间不会在接下来的两个中重复。

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.
    }
}

这些循环在重复:公牛的隔间 1 和 2,奶牛的隔间 1

标签: javascriptarraysloopswebfrontend

解决方案


推荐阅读