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

304
Visualizações
how do I calculate to total amount after removing an item from the cart

My test app is almost complete, however when I remove an "apple" from the cart, and the quantity changes to 2, I can't seem to change the price/total amount from what is when it is three apples.

This may have to do with the constructor class, but I'm still relatively new to using it so I'm having a little trouble implementing a fix for this.

In the console log, I essentially want the total amount to reflect the quantity of apples ( which is now 2).


module.exports = {
    shoppingCart: function () {
        //create empty cart array
        theCart = [];
        // create two seperate versions of the same kind of object using class method 
        class Fruits {
            constructor(fruit, price, quantity) {
                this.fruit = fruit;
                this.quantity = quantity;
                this.price = price * quantity--;

            }
        }
        let fruit1 = new Fruits("Apple", 4.95, 3);       // create new object. sets the value of this
        if (fruit1.quantity === 3) {
            fruit1.quantity--;
        }

        let bothFruits = [fruit1];
        //add items to the cart
        Array.prototype.push.apply(theCart, bothFruits);

        let total = fruit1.price + " = total amount.";

        //function to add items to the cart
        function removeAllItems() {
            if (theCart.length = !0) {
                theCart = [];
            }
        }
        //removeAllItems();  
        console.log(theCart, total);
    }
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is better to define price as unit price, not as total price. You can add more properties, for example cost as the total price of fruit class. Then you can add a method to change the quantity of the fruit.

module.exports = {
    shoppingCart: function () {
        //create empty cart array
        theCart = [];
        // create two seperate versions of the same kind of object using class method 
        class Fruits {
            constructor(fruit, price, quantity) {
                this.fruit = fruit;
                this.quantity = quantity;
                this.price = price;
                this.cost = quantity * price;
            }
            // add method to change fruit quantity
            changeQuantity(newQuantity) {
                this.quantity = newQuantity
                this.cost = this.price * newQuantity;
            }
        }

        let fruit1 = new Fruits("Apple", 4.95, 3);       // create new object. sets the value of this
        if (fruit1.quantity === 3) {
            fruit1.quantity--;
            fruit1.changeQuantity(fruit1.quantity);      // call the method to change the quantity
        }

        let bothFruits = [fruit1];
        //add items to the cart
        Array.prototype.push.apply(theCart, bothFruits);

        let total = fruit1.cost + " = total amount.";

        //function to add items to the cart
        function removeAllItems() {
            if (theCart.length = !0) {
                theCart = [];
            }
        }
        //removeAllItems();  
        console.log(theCart, total);
    }
}

result

[ Fruits { fruit: 'Apple', quantity: 2, price: 4.95, cost: 9.9 } ] 9.9 = total amount.
about 4 years ago · Juan Pablo Isaza 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