JavaScript循环,从总数中减去,直到没有剩余
各位堆垛工人 我不知道如何使我的“bundler”函数循环。我希望能够传递不同的数量,并根据数量决定哪个框/组合是最佳匹配。更大的盒子有更大的折扣。我试图通过减去进入捆绑包的项目数和放入盒子的项目数来实现这一点,并在其中循环,但我不确定我的语法出了什么问题。如有任何反馈,将不胜感激 也就是说,一件东西,只要250美元就可以放进一个盒子里 即22件物品,将装入两个10盒和两个1盒。 即39件物品,分为三个10盒、一个5盒和四个1盒JavaScript循环,从总数中减去,直到没有剩余,javascript,loops,logic,Javascript,Loops,Logic,各位堆垛工人 我不知道如何使我的“bundler”函数循环。我希望能够传递不同的数量,并根据数量决定哪个框/组合是最佳匹配。更大的盒子有更大的折扣。我试图通过减去进入捆绑包的项目数和放入盒子的项目数来实现这一点,并在其中循环,但我不确定我的语法出了什么问题。如有任何反馈,将不胜感激 也就是说,一件东西,只要250美元就可以放进一个盒子里 即22件物品,将装入两个10盒和两个1盒。 即39件物品,分为三个10盒、一个5盒和四个1盒 function product(sku, name, quant
function product(sku, name, quantity, cost) {
this.sku = sku;
this.name = name;
this.quantity = quantity;
this.cost = cost;
}
function bundler(quantity) {
var remainder = quantity;
while (remainder > 0) {
var bundle = [];
switch (true) {
case (quantity >= 10):
box_10 = new product(3333,"Box of 10",1,1500);
bundle.push(box_10);
remainder = quantity - 10;
return bundle;
case (remainder >= 5 && remainder <= 9):
box_5 = new product(2222,"Box of 5",1,1000);
bundle.push(box_5);
remainder = remainder - 5;
return bundle;
case (remainder <=4 && remainder > 0):
bundle = new product(1111,"Box of 1",remainder,250);
remainder = remainder - remainder;
return bundle;
}
}
}
var order = bundler(19);
功能产品(sku、名称、数量、成本){
this.sku=sku;
this.name=名称;
这个。数量=数量;
成本=成本;
}
功能绑定器(数量){
var剩余=数量;
而(余数>0){
var bundle=[];
开关(真){
箱子(数量>=10):
10号框=新产品(3333,“10号框”,11500);
捆绑。推送(盒子10);
余数=数量-10;
返回包;
案例(余数>=5&&余数试试这个
function product(sku, name, quantity, cost) {
this.sku = sku;
this.name = name;
this.quantity = quantity;
this.cost = cost;
}
function bundler(quantity) {
var remainder = quantity;
var bundle = [];
while (remainder > 0) {
if (remainder >= 10){
var box_10 = new product(3333,"Box of 10",1,1500);
bundle.push(box_10);
remainder = remainder - 10;
}
if (remainder >= 5 && remainder <= 9)
{
var box_5 = new product(2222,"Box of 5",1,1000);
bundle.push(box_5);
remainder = remainder - 5;
}
if (remainder <=4 && remainder > 0){
var box_1 = new product(1111,"Box of 1",remainder,250);
bundle.push(box_1);
remainder = remainder - 1;
}
}
return bundle;
}
alert(bundler(22).length);
功能产品(sku、名称、数量、成本){
this.sku=sku;
this.name=名称;
这个。数量=数量;
成本=成本;
}
功能绑定器(数量){
var剩余=数量;
var bundle=[];
而(余数>0){
如果(余数>=10){
var box_10=新产品(3333,“10个盒子”,11500);
捆绑。推送(盒子10);
余数=余数-10;
}
如果(余数>=5&&余数试试这个
function product(sku, name, quantity, cost) {
this.sku = sku;
this.name = name;
this.quantity = quantity;
this.cost = cost;
}
function bundler(quantity) {
var remainder = quantity;
var bundle = [];
while (remainder > 0) {
if (remainder >= 10){
var box_10 = new product(3333,"Box of 10",1,1500);
bundle.push(box_10);
remainder = remainder - 10;
}
if (remainder >= 5 && remainder <= 9)
{
var box_5 = new product(2222,"Box of 5",1,1000);
bundle.push(box_5);
remainder = remainder - 5;
}
if (remainder <=4 && remainder > 0){
var box_1 = new product(1111,"Box of 1",remainder,250);
bundle.push(box_1);
remainder = remainder - 1;
}
}
return bundle;
}
alert(bundler(22).length);
功能产品(sku、名称、数量、成本){
this.sku=sku;
this.name=名称;
这个。数量=数量;
成本=成本;
}
功能绑定器(数量){
var剩余=数量;
var bundle=[];
而(余数>0){
如果(余数>=10){
var box_10=新产品(3333,“10个盒子”,11500);
捆绑。推送(盒子10);
余数=余数-10;
}
如果(余数>=5&&余数Abhishek,非常感谢!将您的代码与我的代码进行比较,看起来我走的是正确的道路,只是有些逻辑放在了错误的地方!Abhishek,非常感谢!将您的代码与我的代码进行比较,看起来我走的是正确的道路,只是有些逻辑放在了错误的地方!为什么不使用模除,一个d Math.floor?将框的类型放入整数数组,并循环遍历其元素。如果Math.floor值大于零,则使用该大小的多个框。如果剩余的框(通过模)大于零,请尝试下一个框大小。将结果存储到新的整数数组中。@cabbagery,我不知道如何做。也许您可以发布一个示例?为什么不使用模除和Math.floor?将框类型放入整数数组,并循环其元素。如果Math.floor值大于零,请使用该many个相同大小的方框。如果余数(通过模)大于零,请尝试下一个方框大小。将结果存储到新的整数数组中。@cabbagery,我不知道如何操作。也许您可以发布一个示例?