I have created a sample shopping cart that stores the data in localStorage
and display it in the cart. The problem where I am stuck is the total of prices as it's kept on concatenating all the price values but not doing the SUM of it. Suppose, two items have prices 100$
& 600$
respectively then the total amount is coming as 100600
.
How can I solve this problem?
HTML code:
<div class="total"></div>
JavaScript:
const cTotal = cartbox.querySelector('.total');
let cartTotal = '';
let price = '';
JSON.parse(localStorage.getItem("items")).map(data=>
{
price = data.price;
for(var i = 0; i < price.length; i++)
{
cartTotal += price[i];
}
});
cartTotal = '<p class="amount">Total Amount: '+cartTotal+'</p>'
cTotal.innerHTML = cartTotal;