Home > Software engineering >  buy x get free y formula
buy x get free y formula

Time:11-23

Trying to find out how to formulating buy x get y free. Example buy 10 get 1 free, buy 20 get 2 free and so on. For now I just use like like this formula. This formula will not working when change to example: Buy 5 get 1 free, buy 10 get 2 free..

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;

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

CodePudding user response:

Simple formula:

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

Change parameters to fit your BOGO rate

CodePudding user response:

The problem is that you have not specified it as a function. If you want a function, add function:

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

            }
          }
        });
      });
    });
  
  }
  • Related