I just wanna know what is the best data structure type or any idea for executing multiple promo codes for discounts in the right order. For example I may have $164.99 worth of items in my shopping basket. After I apply promo code that takes 5% off the initial price and then I apply -20 dollars promo code I get 136.74 as the final price. Now let's say I wanna switch the order promotions have been added to the basket. On the initial price of $164.99 I first want to apply -20 dollars promo code and then 5% promo code and I get the 137.74 as the final price. How can I make it always have the right price in this case it would be $136.74 let's say.
I've only applied one switch case for that and here it is :
export const priceAfterDiscount = (cart, promotions) => {
let totalReturnedPrice = getCartTotal(cart);
promotions.forEach(element => {
switch(element){
case "5%OFF":
var reducedPrice = (totalReturnedPrice * 5) / 100;
totalReturnedPrice -= reducedPrice;
break;
case "20EUROFF":
totalReturnedPrice -= 20;
break;
case "20%OFF":
var reducedPrice = (totalReturnedPrice * 20) / 100;
totalReturnedPrice -= reducedPrice;
break;
}
});
return totalReturnedPrice;
}
Juan Pablo Isaza
All you can do is to apply every discount to the original value.
-say 5% applied on $164.99. so discount will be of $8.2495, you need to save this value
second discount is of $20 so total discount becomes $(20+8.2495) $164.99-28.2495
Hence the discount remained same irrespective to the order of promo