1

I have the following code

 function getNumberWithCommas(number) { return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "."); } var price = [ 6599, 7659, 899, 569, 899, 279, 219, 1797, 3999, 2769, 599, 1349, 2149, 219, 219, 519, 2499, 2949, 5149, 7689, 15999, ]; for (let i = 0; i < 20; i++) { var productPrice = document.createElement("span"); productPrice.className = "price"; productPrice.setAttribute("id", `price-id-${i}`); var calculatedPrice = price[i] * quantities[i]; productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`; precoTotalDiv.appendChild(productPrice); var totalPrice = []; totalPrice.push(calculatedPrice); console.log(totalPrice); } 

It dynamically creates prices for products based on their quantity in the cart and their price. I would like to save these values (calculatedPrice) inside an array so I can get a total price (sum of all the prices) at the bottom of the cart.

It is adding calculatedPrice to totalPrice[], but it only adds one value, the length of the array remains one. Once I click another product, calculatedPrice gets overwritten with another products price. I would like to get a sum of all the prices generated, not the sum of only one product.

8
  • So put array.push(calculatedPrice) in the loop. Commented Feb 10, 2023 at 1:55
  • Why do you need an array to calculate the total? Just put total += calculatedPrice in the loop. Commented Feb 10, 2023 at 1:56
  • BTW, you're never appending the span to the DOM. Commented Feb 10, 2023 at 1:56
  • I tried, but the saved value gets reset to another product's price. Commented Feb 10, 2023 at 1:57
  • 3
    You create a new array on each iteration... Commented Feb 10, 2023 at 2:16

3 Answers 3

1

just move let totalPrice = [] to outside of the loop:

Because when you put totalPrice inside the loop and it will be reset value on each loop

 var totalPrice = []; for (let i = 0; i < 20; i++) { var productPrice = document.createElement("span"); productPrice.className = "price"; productPrice.setAttribute("id", `price-id-${i}`); var calculatedPrice = price[i] * quantities[i]; productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`; precoTotalDiv.appendChild(productPrice); totalPrice.push(calculatedPrice); } console.log(totalPrice); 
Sign up to request clarification or add additional context in comments.

Comments

1

This is because you're creating a new array on each loop

Declare your array outside of the loop first:

 var totalPrice = []; for (let i = 0; i < 20; i++) { var productPrice = document.createElement("span"); productPrice.className = "price"; productPrice.setAttribute("id", `price-id-${i}`); var calculatedPrice = price[i] * quantities[i]; productPrice.textContent = `R$ ${getNumberWithCommas(calculatedPrice)},00`; precoTotalDiv.appendChild(productPrice); totalPrice.push(calculatedPrice); console.log(totalPrice); } 

Comments

1

You can save the values generated by a for loop inside an array in JavaScript by using the push method of an array. Here's an example:

// Initialize an empty array var result = []; // Use a for loop to generate values for (var i = 0; i < 5; i++) { // Push the generated value to the array result.push(i); } // Print the final array console.log(result); 

This will output:

[0, 1, 2, 3, 4] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.