I am having an issue with calculating the total order prices (in USD) in an object array. Instead of summing the total, the order totals are being concatenated. How can I ensure that addition is performed instead of concatenation?
<body>
<p id=sales></p>
<script>
var sales, i, x = "";
sales = {"orders": [{"total_price_usd": "92.05"}, {"total_price_usd": "14.90"}, {"total_price_usd": "17.90"}, {"total_price_usd": "14.90"}]}
for (i in sales.orders) {
x += sales.orders[i].total_price_usd + ', ';
}
var numbers = [x];
function getSum(total, num) {
return parseFloat(total) + parseFloat(num);
}
document.getElementById('sales').innerHTML = '$' + numbers.reduce(getSum);
</script>
</body>