buy x get free y formula


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;

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

Source: JavaSript – Stack Overflow

November 23, 2021
Category : News
Tags: angular | javascript | math | typescript

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.