title: JavaScript Loop, subtracting from a total until nothing remains
body: <p>Fellow Stackers,</p>
  
  <p>I am not sure how to make my "bundler" function loop.  I want the ability to pass it different quantities and based on the quantities decide which box/combo is the best match.  Bigger boxes have a bigger discount.  I tried to accomplish this by subtracting how many items came into the bundle vs how many got put into a box and loop through it, but I'm not sure what's wrong with my syntax.  Any feedback would be appreciated.</p>
  
  <p>Ie, 1 item, would go into a 1 Box for $250
  Ie. 22 items, would go into two 10 Box, and two 1 Box.
  Ie. 39 items, would go into three 10 box, one 5 box, and four 1 Box</p>
  
  <pre><code>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 &gt; 0) {
          var bundle = [];
          switch (true) {
          case (quantity &gt;= 10):
            box_10 = new product(3333,"Box of 10",1,1500);
            bundle.push(box_10);
            remainder = quantity - 10;
            return bundle;
          case (remainder &gt;= 5 &amp;&amp; remainder &lt;= 9):
            box_5 = new product(2222,"Box of 5",1,1000);
            bundle.push(box_5);
            remainder = remainder - 5;
            return bundle;
          case (remainder &lt;=4 &amp;&amp; remainder &gt; 0):
            bundle = new product(1111,"Box of 1",remainder,250);
            remainder = remainder - remainder;
            return bundle;
          }
        }
      }
  
      var order = bundler(19);
  </code></pre>