买x送y公式

buy x get free y formula

试图了解如何制定买 x 送 y。例如买10送1,买20送2等等。现在我只是像这样使用这个公式。换个例子这个公式就不行了:买5送1,买10送2..

Ts

getFreeItem(){
    
    let countTotal;

    this.commonService.getOrderList$.subscribe((x) => {
      x.filter(item => {
        
        item.forEach((element, i) => {
          
          if (element.freeDealMin !== null) {
            item?.reduce((a, b) => {
              let aVal = a?.count ? a?.count : a;
              let bVal = b?.count ? b?.count : b;
             
              countTotal = aVal + bVal;
              
              return aVal + bVal;
            }, 0);

            let statusFree = element.freeDealMin/ countTotal;
            
            if (statusFree == 1) {
             
                this.freeDealQty = 1;
             
            }else if (statusFree == 0.5) {
              
                this.freeDealQty = 2;

            }else if (statusFree == 0.33333333333) {
              
                this.freeDealQty = 3;

            }
          }
        });
      });
    });
  
  } 

简单公式:

    function getFreeItem(purchaseCount,buyCount=10,freeCount=1) { // default paramters (10 items for 1 free)
      let rate = Math.floor(purchaseCount / buyCount);
      return rate * freeCount;
    }
    
    console.log('Buy 10, Get 1');
    console.log('5', getFreeItem(5));
    console.log('10',getFreeItem(10));
    console.log('15',getFreeItem(15));
    console.log('20',getFreeItem(20));
    console.log();
    
    console.log('Buy 5, Get 1');
    console.log('5', getFreeItem(5,5));
    console.log('10',getFreeItem(10,5));
    console.log('15',getFreeItem(15,5));
    console.log('20',getFreeItem(20,5));
    console.log();
    
    console.log('Buy 10, Get 2');
    console.log('5', getFreeItem(5, 10,2));
    console.log('10',getFreeItem(10,10,2));
    console.log('15',getFreeItem(15,10,2));
    console.log('20',getFreeItem(20,10,2));
    console.log();
    
    console.log('BOGO: Buy 1, Get 1');
    console.log('5', getFreeItem(5, 1));
    console.log('10',getFreeItem(10,1));
    console.log('15',getFreeItem(15,1));
    console.log('20',getFreeItem(20,1));
    console.log();

更改参数以适合您的 BOGO 率