javascript - 我需要帮助将拖车隔间分配给动物
问题描述
我们的拖车有 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
解决方案
推荐阅读
- java - Eclipse RAP 应用程序中的注销处理
- css - 如何制作按钮内容高亮动画?
- discord.py - 在 discord.py 嵌入中向下滚动
- angular - 在特定部门外键中显示员工
- sql - 如果它的类型是日期时间,我们如何添加 NULL 作为雪花上接受空值的字段的值
- javascript - 用户停止滚动延迟后如何移动导航栏(JavaScript)
- image - ValueError: 层序的输入 0 与层不兼容
- node.js - 我面临 Heroku 部署问题,npm start 无法正常工作
- java - 可点击目标
- python - 多列的 Pandas Cumcount()