JSFiddle - React, Tailwind, and code Playground

by akang2

JavaScript

//Act 6.2 Totaling Values

var cart = [
    1.99,
    2.50,
    9.25,
    4.88
];

//Here I'm defining a variable to store the total and calling it 'total'
//We have to set the initial value equal to 0 because if we try to do addition to an undefined value, it'll give us 'NaN'. 
var total=0;

//Here I'm writing a loop to go through each value and add it to the total
for (i=0; i < cart.length; i++){

    total = total + cart[i];
    console.log(total);
}