Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

144
Visualizações
Applying promotions for a shoppin cart in react in the right order

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;
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda