1

堆垛机同胞,

我不确定如何使我的“捆绑器”功能循环。我希望能够传递不同的数量,并根据数量决定哪个盒子/组合是最佳匹配。更大的盒子有更大的折扣。我试图通过减去进入捆绑包的项目数量与放入盒子中的项目数量并循环遍历它来实现这一点,但我不确定我的语法有什么问题。对于任何反馈,我们都表示感谢。

即,1 件物品将以 250 美元的价格放入 1 个盒子中。22 件商品,将分为两个 10 盒和两个 1 盒。IE。39 件,将分为三个 10 盒、一个 5 盒和四个 1 盒

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);
4

1 回答 1

1

试试这个;

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

小提琴

于 2013-08-07T07:10:09.867 回答