用于计算产品价格的JavaScript构建和算法

时间:2015-03-10 20:54:54

标签: javascript

我正在开发一个小网站,我有20个产品销售。

每周我想计算每种产品的新价格,同时考虑到每种产品的销售数量,但遵循两条规则:

1 - 20种产品的价格总和必须为100€

2 - 我希望每个产品都是整数(无小数)或者是.5,我的意思是,一个产品的价格可以是7或8或9或7.5或8.5但不是8.3或7.1。

产品的3-最低价格是1.5。

让我们做一个例子,但简化了4种产品:

本周卖出:

product_id  sells
----------  ------
     1       45
     2       31
     3       12
     4       62
好的,总销售额是150。

如果我将这100欧元除以150卖出,我得到0.667欧元/卖出。所以如果我将每个产品乘以这个值,我会得到:

product_id  sells  raw_price_next_week
----------  ------  -------------------
     1       45             30.015
     2       31             20.677
     3       12             8.004
     4       62             41.354

然后我需要将这些raw_prices标准化,考虑到我之前提到的第二个规则,得到每个raw_value最接近的0.5倍......

product_id  sells  raw_price_next_week    final_price
----------  ------  -------------------    ------------
     1       45             30.015             30
     2       31             20.677             20.5
     3       12             8.004              8
     4       62             41.354             41.5

好的我有一个代码可以做到一切正常,除了第三点,最低价格为1.5。因此,例如,如果产品的原始价格为0.5,则价格应为1.5,因为它是最低价格。

我想当时要做的事情应该是计算在原始价格低于1.5时调整到最低1.5欧元的产品花费多少欧元,并将调整后的价格总和存储在变量中的1.5 (想象6€)。之后,100€ - 6€= 94€。有了这个数量,剩下的算法......

这是到目前为止的代码:

// Start with the raw product data (array of object - from database likely)
    var 
        products = [
            {
                product_id: 1,
                sells: 45
            },
            {
                product_id: 2,
                sells: 31
            },
            {
                product_id: 3,
                sells: 12
            },
            {
                product_id: 4,
                sells: 62
            },
            {
                product_id: 5,
                sells: 1
            },
            {
                product_id: 6,
                sells: 2
            }
        ]
    ;

    // ###############################################################

window.Pricing = {

    total_sum: 100, // This represents the 100€
    sales : 0,
    min_price : 1.5

    init: function( products ) {     
        return this.calculate_final_price( products );
    },

    get_price_per_sale: function( products ) {

        for( x in products ) {
            this.sales += products[x].sells;        
        }

        return this.total_sum / this.sales;                
    },

    calculate_final_price: function( products ) {

        var price_per_sale = this.get_price_per_sale( products );

        for( x in products ) {
            products[x].raw_price_next_week = +( products[x].sells * price_per_sale );
            products[x].final_price = +( Math.round( products[x].raw_price_next_week * 2 ) / 2 ).toFixed(1);        
        }

        return products;
    }

};

// ###############################################################

// Init the pricing calculations and get the final objects with updated pricing.
Pricing.init( products );

1 个答案:

答案 0 :(得分:1)

我添加了一些用于验证的变量,然后让函数重新调用它直到找到有效的解决方案。

// Start with the raw product data (array of object - from database

var products = [ { product_id: 1, sells: 45 }, { product_id: 2, sells: 31 }, { product_id: 3, sells: 12 }, { product_id: 4, sells: 62 }, { product_id: 5, sells: 1 }, { product_id: 6, sells: 2 } ] ;
var Pricing = {

    necessary_sum: 100,
    dummy_sum: 100, // This represents the 100€
    sales : 0,
    min_price : 1.5,
    real_sum : 0,

    init: function( products ) {     
        return this.calculate_final_price( products );
    },

    get_price_per_sale: function( products ) {
        this.sales = 0;

        for( x in products ) {
            this.sales += products[x].sells;        
        }

        return this.dummy_sum / this.sales;                
    },

    calculate_final_price: function( products ) {

        var price_per_sale = this.get_price_per_sale( products );

        for( x in products ) {
            products[x].raw_price_next_week = products[x].sells * price_per_sale;
            products[x].final_price = (Math.round( products[x].raw_price_next_week * 2 ) / 2 ).toFixed(1);        

            if(products[x].final_price < this.min_price){
                products[x].final_price = this.min_price;
            }

            this.real_sum += ~~products[x].final_price;
        }

        if(this.real_sum != this.necessary_sum){
            this.dummy_sum -= this.real_sum - this.necessary_sum;
            this.real_sum = 0;
            price_per_sale = this.get_price_per_sale( products );
            this.calculate_final_price(products);
        }

        return products;
    }

};

// Init the pricing calculations and get the final objects with updated pricing.
console.log(Pricing.init( products ));
console.log('should be: ' + Pricing.necessary_sum);
console.log('really: ' + Pricing.real_sum);