parsing numbers in a javascript array
- by George
Hi I have a string of numbers separated by commas, "100,200,300,400,500" that I'm splitting into an array using the javascript split function:
var data = [];
data = dataString.split(",");
I'm trying to parse the values of the array using parseFloat and then store them back into the array. I'd then like to add up the numbers in the array and store it as another variable, "dataSum".
I've got the following code, but I can't get it working:
var dataSum = "";
for (var i=0; i < data.length; i++) {
parseFloat(data[i]);
dataSum += data[i];
}
So at the end of all this, I should be able to access any of the parsed numbers individually data[0], data[1], etc... and have a total number for dataSum. What am I doing wrong?