1

I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work:

   var total = 0;

   for (var i  = 0; i < totalPrice.length; i++);

   total  += totalPrice[i];
   document.getElementById("displayPrice").innerHTML = total;

But total comes out as a NaN.

This is an example of my code in JSFiddle, if you add the item twice the value gets pushed into the array, what am I doing wrong with the for loop?

https://jsfiddle.net/bgv5s9re/2/

Sergi
  • 1,131
  • 3
  • 15
  • 32

1 Answers1

4

You could use brackets

  var total = 0;

   for (var i  = 0; i < totalPrice.length; i++){

      total  += totalPrice[i];

   }
   document.getElementById("displayPrice").innerHTML = total;
ScaisEdge
  • 129,293
  • 10
  • 87
  • 97
  • While I use that style, it's no more proper than using any other statement. Proper is defined by the spec, and any statement in that position constitutes a proper loop. –  Oct 08 '16 at 15:58
  • 1
    @squint answer updated – ScaisEdge Oct 08 '16 at 15:59