I'm just getting started with programming and teaching myself. I'm struggling to grasp how to update a variable by iterating through an array.
Here's an array containing the best pies, each with its own price:
[blueberry, strawberry, pumpkin, apple]
I want the total to increment based on the price using conditional statements, like this:
var total = 0
arr.map(pie => {
if (pie==="blueberry") {
total = total + 2.5;}
else if (pie === "apple") {
total = total + 2}}
However, the total only reaches 2.5 before resetting to 0. So instead of 4.5, I end up with a total of 2 because "apple" was the last pie. :(
What causes this issue, and how can I ensure the total is preserved and continues to accumulate the prices of the other pies?