parsing numbers in a javascript array
Posted
by George
on Stack Overflow
See other posts from Stack Overflow
or by George
Published on 2010-04-05T19:36:46Z
Indexed on
2010/04/05
19:43 UTC
Read the original article
Hit count: 436
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?
© Stack Overflow or respective owner