0

thank you for your previous help, now the toal quantity works perfectly. i have tried to aplly the same principle with the total price but it displays "nan" (" price += parseInt(monPanier[i].price);"). before adding this line, the total price diplayed was only one copy per item even eg there was two black items, 3 red items and 4 yellow, it was considered as total price one black + one red + one yellow. Thank you for your help.

 let asynCompteur = 0;
    let quantity = 0;
    let price = 0;
    for (let i = 0; i < monPanier.length; i++) {
        fetch('http://localhost:3000/api/products/' + monPanier[i].id)/*appel de l'api*/
        .then((reponse) => reponse.json())
        .then((data) => { 
            /*asynCompteur pour réguler l'itération*/
            asynCompteur = asynCompteur + 1;              
            //J'ajoute le prix et la quantité
            quantity += parseInt(monPanier[i].quantity);                price += data.price;
            console.log('prix', asynCompteur, price, quantity);
            price += parseInt(monPanier[i].price);
            console.log(typeof monPanier[i].quantity);
            if (asynCompteur === monPanier.length) {
                document.querySelector("#totalQuantity").innerText = quantity;
                document.querySelector("#totalPrice").innerText = price; 
            }

6
  • 1
    Try to use parseInt(asynCompteur) + 1 when you add Commented May 26, 2022 at 12:51
  • 2
    try quantity += parseInt(monPanier[i].quantity,10); Commented May 26, 2022 at 12:51
  • As far as I can see, the only way this could be a string is when monPanier[i].quantity is a string. What does console.log(typeof monPanier[i].quantity) prints? Commented May 26, 2022 at 12:51
  • Clearly monPanier[i].quantity is type string. Conversion to integer is necessary since Javascript string concatenation operator + is the same as, and takes precedence over, integer addition. As already mentioned parseInt is one solution. Another is the unary operator quantity += +monPanier[i].quantity; Commented May 26, 2022 at 13:12
  • Does this answer your question? Addition not working in Javascript Commented May 26, 2022 at 13:17

1 Answer 1

0

it works partially. The quantity is updated correctly and follow the changes of the cart.So, the display of total quantity is correct.But... strangely, the total price consider only one of each item, it means: ie black item (2 times), yellow item (3 times), red item (2 times), total quantity displayd: 7 (correct), total price: incorrect(it displays total price of one black item + one yellow item + one red item. Thank you for your precious help.

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.