javascript - 根据多个变量(范围、增量、层级)计算价格
问题描述
我有一个定价计算器,需要根据用户输入考虑这些变量:价格层、价格范围和价格增量。每一层导致不同的价格范围和不同的价格增量。
计算器应考虑特定的“联系电话”,如 10k、50k 等。让我们以 10000 个联系人为例。这些可以按市场层级(入门级、专业级、企业级)、范围(多少联系人应该花费 0 美元、50 美元等)和增量(入门级 1k 增量,专业级 5k 增量和企业级 10k 增量)来划分)。
例如,如果我有 10k 联系人包含在市场层 Starter 中,并且有 1k 增量,我需要有 390 美元的总和:
0 -> 1000 = 0 USD
1000 -> 2000 = 50 USD
2000 -> 3000 = 50 USD
3000 -> 4000 = 45 USD
4000 -> 5000 = 45 USD
5000 -> 6000 = 40 USD
6000 -> 7000 = 40 USD
7000 -> 8000 = 40 USD
8000 -> 9000 = 40 USD
9000 -> 10000 = 40 USD
total = 390 USD
如果我有 10k 联系人,并用作市场层 Pro 及其相应的范围,我将有 5k 的增量,总和应该是 500 美元。以此类推,增量为 10K。
// this is the first tier with prices for each range;
let marketingStarter = [{
1000: {
usd: 0,
eur: 0,
},
},
{
3000: {
usd: 50,
eur: 46,
},
},
{
5000: {
usd: 45,
eur: 41,
},
},
{
7000: {
usd: 40,
eur: 36,
},
},
{
9000: {
usd: 40,
eur: 36,
},
},
{
9001: {
usd: 40,
eur: 36,
},
},
];
[...]
const priceRangeStarter = [1000, 3000, 5000, 7000, 9000, 9001];
// second and third price range
const priceRangePro = [2000, 2200, 42000, 62000, 82000, 82001];
const priceRangeEnt = [10000, 50000, 100000, 200000, 500000, 500001];
// price should be calculated in increments of 1k for starter, 5k for pro, and 10k for Ent;
const priceIncrements = [1000, 5000, 10000];
这是我尝试过的:
function marketingPricing() {
let currency = "usd";
let contacts = 10000;
let pricePerContact,
sum = 0,
totalPrice;
for (let i = 0; i < priceRangeStarter.length; i++) {
if (contacts >= priceRangeStarter[i]) {
// get price per contact from array
pricePerContact = marketingStarter[i][priceRangeStarter[i]][currency];
// 0 - 1000 = 0 USD
// 1000 - 3000 = 50 USD
// 3000 - 5000 = 45 USD
// 5000 - 7000 = 40 USD
// 7000 - 9000 = 40 USD
// 9001 --- 40 USD
// calculate rest of contacts
contacts = contacts - priceRangeStarter[i];
// 1: 9000;
// 2: 6000;
// 3: 1000;
// price is per 1K
totalPrice = pricePerContact * (priceRangeStarter[i] / priceIncrements[0]);
// 1: 50 USD * 1000 / 1000 = 50; (should be 0)
// 2: 45 USD * 3000 / 1000 = 135; (should be 90)
// 3: 40 USD * 1000 / 1000 = 40; (correct)
sum = sum + totalPrice;
// 0 -> 1000 = 0 USD
// 1000 -> 2000 = 50 USD
// 2000 -> 3000 = 50 USD
// 3000 -> 4000 = 45 USD
// 4000 -> 5000 = 45 USD
// 5000 -> 6000 = 40 USD
// 6000 -> 7000 = 40 USD
// 7000 -> 8000 = 40 USD
// 8000 -> 9000 = 40 USD
// 9000 -> 10000 = 40 USD
// total = 390 USD
} else {
pricePerContact = marketingStarter[i][priceRangeStarter[i]][currency];
totalPrice = pricePerContact * (contacts / priceIncrements[0]);
contacts = 0;
sum = sum + totalPrice;
}
}
}
任何帮助表示赞赏。你可以在这里找到一个小提琴:https ://jsfiddle.net/rnef1hdo/6/
解决方案
经过几天的努力,我找到了使用原始解决方案获得正确结果的解决方案。我承认我应该更好地解释问题。
我所做的不同之处在于:我添加了previousRange
能够计算付费联系人号码的功能。我已经添加Math.ceil
了最后的联系人,以四舍五入最终价格。最重要的一步,也是问题解决者:我添加了一个priceRangeInterval
具有范围差异的新变量。这些也是 for 循环中每个步骤的付费联系人。所以我替换了from to中的if
条件。然后一切都变得有意义,并按其应有的方式工作。for loop
priceRange
priceRangeInterval
这是解决方案:
const priceRangeStarter = [1000, 3000, 5000, 7000, 9000, 9001];
const priceRangePro = [2000, 22000, 42000, 62000, 82000, 82001];
const contactsBand = [1000, 5000, 10000];
const priceRangeIntervalStarter = 2000;
const priceRangeIntervalPro = 20000;
const priceRangeIntervalEnt = [10000, 40000, 50000, 100000, 400000];
function marketingPricing() {
let currency = "usd";
let priceRange = priceRangePro;
let marketTier = marketingPro;
let priceRangeInterval = priceRangeIntervalPro;
let contacts = 98000;
let contactsBandTier = contactsBand[1];
let pricePerContact,
sum = 0,
totalPrice;
for (let i = 0; i < priceRange.length; i++) {
if (priceRangeInterval === priceRangeIntervalEnt) {
priceRangeInterval = priceRangeIntervalEnt[i];
}
if (contacts >= priceRangeInterval && i < (priceRange.length - 1)) {
// get price per contact from array
pricePerContact = marketTier[i][priceRange[i]][currency];
// calculate rest of contacts
let previousPriceRange = 0;
if (i != 0) {
previousPriceRange = priceRange[i - 1];
}
contacts = contacts - (priceRange[i] - previousPriceRange);
totalPrice = pricePerContact * ((priceRange[i] - previousPriceRange) / contactsBandTier);
sum = sum + totalPrice;
} else {
pricePerContact = marketTier[i][priceRange[i]][currency];
totalPrice = pricePerContact * Math.ceil(contacts / contactsBandTier);
contacts = 0;
sum = sum + totalPrice;
}
}
}
这是一个工作小提琴:https ://jsfiddle.net/54pkgvd9/1/
感谢所有花时间尝试理解并帮助我解决这个问题的人。
推荐阅读
- datetime - 将publishAt(API TIME)转换为正常时间
- python - 如何使用 Python REGEX 重新排序字符串(不管它是怎样的)
- sql - 查询以仅提取相关的列或具有特定 where 条件的数据
- python - QT Opencv人脸检测在Python中不起作用?
- javascript - antialias=false 在 MacOS 上使用 Safari 阻碍 readPixels
- java - Android:无论发生什么变化,xml都不会更新运行时
- nlp - 是否有公认的策略来获得与 NLP 中的人或地点相关的形容词(或对人或地点的一般印象)?
- c# - 有没有办法简化重复的吸气剂?
- r - 为什么 html 表不给我 p 值并添加 -9 值?表格1
- javascript - TypeError:未定义不是对象(评估“this.state.imagesID”)