gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:36:09 25 4
gpt4 key购买 nike

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

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

1 - 20 种产品的价格总和必须始终为 100 欧元

2 - 我希望每个产品都是整数(无小数)或 0.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 欧元的产品花费了多少欧元,并将调整后价格的总和存储在 var 中(想象一下 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 );

最佳答案

我添加了一些变量进行验证,然后让函数自行调用,直到得出有效的解决方案。

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

关于用于计算产品价格的 JavaScript 构建和算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com