I am NOT talking about concatenating elements together, but adding their values to another separate variable.
Like this:
var TOTAL = 0;
for (i=0; i<myArray.length; i++) {
TOTAL += myArray[i];
}
With this code, TOTAL doesn’t add mathematically element values together, but it concatenates them next to each other, so if myArray[0] = "10" and myArray[1] = "10" then TOTAL will be "01010" instead of 20.
How should I write what I want?
Thanks
Sounds like your array elements are Strings, try to convert them to Number when adding:
Note that I use the unary plus operator (
+myArray[i]), this is one common way to make sure you are adding up numbers, not concatenating strings.