Javascript 用于在促销活动中应用多个折扣的算法
Javascript Algorithm to use for applying multiple discounts in a promotional sales event
我们的商店有促销活动,如果您购买 1 个键盘,则可享受 20% 的折扣,如果您购买 2 个键盘,可享受 30% 的折扣,购买 3 个键盘,可享受 40% 的折扣。鉴于键盘的产品价格为 199.99 美元。可申请折扣的键盘数量上限为6个,购买超过6个将不再享受额外折扣。
谁能帮我创建一个算法来应用折扣并计算客户购买 1 至 6 件或更多该特定产品时的总金额?
Buy 1 Keyboard = 20% discount
Buy 2 Keyboards = 30% discount
Buy 3 Keyboards = 40% discount
这是我尝试解决问题的第一个代码,但我得到的结果不正确:
let cart = [];
let disc_amount;
let total_amount;
discFor1 = 0.2;
discFor2 = 0.3;
discFor3 = 0.4;
discFor4 = 0.6;
discFor5 = 0.7;
discFor6 = 0.8;
const keyboard = {
id: "1001",
name: "Yamaha PSR-E273 Keyboard",
reg_price: 199.99,
disc_price: 0
};
const addKeyboardToCart = () => {
cart.push(keyboard);
};
const getDiscount = (cart) => {
if (cart.length < 1) {
console.log("Please add an item");
}
if (cart.length === 1) {
disc_amount = keyboard.reg_price * discFor1;
} else if (cart.length === 2) {
disc_amount = keyboard.reg_price * discFor2;
} else if (cart.length === 3) {
disc_amount = keyboard.reg_price * discFor3;
} else if (cart.length === 4) {
disc_amount = keyboard.reg_price * discFor4;
} else if (cart.length === 5) {
disc_amount = keyboard.reg_price * discFor5;
} else if (cart.length === 6) {
disc_amount = keyboard.reg_price * discFor6;
} else if (cart.length > 6) {
disc_amount = keyboard.reg_price * discFor6;
}
return disc_amount;
};
const getTotalAmount = (disc_amount, cart) => {
let total_reg_price_amount = 0;
for (let i = 0; i < cart.length; i++) {
total_reg_price_amount += cart[i].reg_price;
}
total_amount = total_reg_price_amount - disc_amount;
return total_amount;
};
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
getDiscount(cart);
getTotalAmount(disc_amount, cart);
编辑:我也在尝试实现如何为客户获得最佳折扣,这就是为什么我尝试动态应用折扣,而不是对订购的每件商品的折扣金额进行硬编码。
编辑:
我修改了我的问题并删除了其他问题,以便答案可以更专注于特定问题。
我稍微简化了问题,但保持了您所遵循的相同逻辑:
我还添加了小测试功能来测试您想要的所有情况的功能。我已经添加了一些。如果需要,您可以添加更多。
编辑添加:如何计算总价并添加更多测试用例
keyboard = {
reg_price: 200
}
const discFor1 = 0.20
const discFor2 = 0.30
const discFor3 = 0.40
const getDiscount = (cart_length) => {
switch ( cart_length ) {
case 1 : return keyboard.reg_price * discFor1;
case 2 : return keyboard.reg_price * 2 * discFor2;
case 3 : return keyboard.reg_price * 3 * discFor3;
case 4 : return keyboard.reg_price * 3 * discFor3 + keyboard.reg_price * discFor1;
case 5 : return keyboard.reg_price * 3 * discFor3 + keyboard.reg_price * 2 * discFor2;
default : return keyboard.reg_price * 6 * discFor3;
}
};
const getTotalAmount = (cart) => {
if (cart.length == 0) {
console.log("Please add an item")
return;
}
total_amount = cart.length * keyboard.reg_price - getDiscount(cart.length);
return total_amount;
};
const test = ( cart, ans ) => {
if ( getTotalAmount(cart) !== ans) {
console.log(`Error: expected ${ans}, received ${getTotalAmount(cart)}`)
} else {
console.log(`Success: expected ${ans}, received ${getTotalAmount(cart)}`)
}
}
test([], undefined)
test(["keybd"], 200 - 40)
test(["keybd", "keybd"], 400 - 120)
test(["keybd", "keybd", "keybd"], 600 - 240)
test(["keybd", "keybd", "keybd", "keybd"], 800 - 280)
test(["keybd", "keybd", "keybd", "keybd", "keybd"], 1000 - 360)
test(["keybd", "keybd", "keybd", "keybd", "keybd", "keybd"], 1200 - 480)
test(["keybd", "keybd", "keybd", "keybd", "keybd", "keybd", "keybd"], 1400 - 480)
test(["keybd", "keybd", "keybd", "keybd", "keybd", "keybd", "keybd", "keybd"], 1600 - 480)
我们的商店有促销活动,如果您购买 1 个键盘,则可享受 20% 的折扣,如果您购买 2 个键盘,可享受 30% 的折扣,购买 3 个键盘,可享受 40% 的折扣。鉴于键盘的产品价格为 199.99 美元。可申请折扣的键盘数量上限为6个,购买超过6个将不再享受额外折扣。
谁能帮我创建一个算法来应用折扣并计算客户购买 1 至 6 件或更多该特定产品时的总金额?
Buy 1 Keyboard = 20% discount
Buy 2 Keyboards = 30% discount
Buy 3 Keyboards = 40% discount
这是我尝试解决问题的第一个代码,但我得到的结果不正确:
let cart = [];
let disc_amount;
let total_amount;
discFor1 = 0.2;
discFor2 = 0.3;
discFor3 = 0.4;
discFor4 = 0.6;
discFor5 = 0.7;
discFor6 = 0.8;
const keyboard = {
id: "1001",
name: "Yamaha PSR-E273 Keyboard",
reg_price: 199.99,
disc_price: 0
};
const addKeyboardToCart = () => {
cart.push(keyboard);
};
const getDiscount = (cart) => {
if (cart.length < 1) {
console.log("Please add an item");
}
if (cart.length === 1) {
disc_amount = keyboard.reg_price * discFor1;
} else if (cart.length === 2) {
disc_amount = keyboard.reg_price * discFor2;
} else if (cart.length === 3) {
disc_amount = keyboard.reg_price * discFor3;
} else if (cart.length === 4) {
disc_amount = keyboard.reg_price * discFor4;
} else if (cart.length === 5) {
disc_amount = keyboard.reg_price * discFor5;
} else if (cart.length === 6) {
disc_amount = keyboard.reg_price * discFor6;
} else if (cart.length > 6) {
disc_amount = keyboard.reg_price * discFor6;
}
return disc_amount;
};
const getTotalAmount = (disc_amount, cart) => {
let total_reg_price_amount = 0;
for (let i = 0; i < cart.length; i++) {
total_reg_price_amount += cart[i].reg_price;
}
total_amount = total_reg_price_amount - disc_amount;
return total_amount;
};
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
addKeyboardToCart();
getDiscount(cart);
getTotalAmount(disc_amount, cart);
编辑:我也在尝试实现如何为客户获得最佳折扣,这就是为什么我尝试动态应用折扣,而不是对订购的每件商品的折扣金额进行硬编码。
编辑: 我修改了我的问题并删除了其他问题,以便答案可以更专注于特定问题。
我稍微简化了问题,但保持了您所遵循的相同逻辑:
我还添加了小测试功能来测试您想要的所有情况的功能。我已经添加了一些。如果需要,您可以添加更多。
编辑添加:如何计算总价并添加更多测试用例
keyboard = {
reg_price: 200
}
const discFor1 = 0.20
const discFor2 = 0.30
const discFor3 = 0.40
const getDiscount = (cart_length) => {
switch ( cart_length ) {
case 1 : return keyboard.reg_price * discFor1;
case 2 : return keyboard.reg_price * 2 * discFor2;
case 3 : return keyboard.reg_price * 3 * discFor3;
case 4 : return keyboard.reg_price * 3 * discFor3 + keyboard.reg_price * discFor1;
case 5 : return keyboard.reg_price * 3 * discFor3 + keyboard.reg_price * 2 * discFor2;
default : return keyboard.reg_price * 6 * discFor3;
}
};
const getTotalAmount = (cart) => {
if (cart.length == 0) {
console.log("Please add an item")
return;
}
total_amount = cart.length * keyboard.reg_price - getDiscount(cart.length);
return total_amount;
};
const test = ( cart, ans ) => {
if ( getTotalAmount(cart) !== ans) {
console.log(`Error: expected ${ans}, received ${getTotalAmount(cart)}`)
} else {
console.log(`Success: expected ${ans}, received ${getTotalAmount(cart)}`)
}
}
test([], undefined)
test(["keybd"], 200 - 40)
test(["keybd", "keybd"], 400 - 120)
test(["keybd", "keybd", "keybd"], 600 - 240)
test(["keybd", "keybd", "keybd", "keybd"], 800 - 280)
test(["keybd", "keybd", "keybd", "keybd", "keybd"], 1000 - 360)
test(["keybd", "keybd", "keybd", "keybd", "keybd", "keybd"], 1200 - 480)
test(["keybd", "keybd", "keybd", "keybd", "keybd", "keybd", "keybd"], 1400 - 480)
test(["keybd", "keybd", "keybd", "keybd", "keybd", "keybd", "keybd", "keybd"], 1600 - 480)