# How do I add the numbers of an array

I have a list consisting of numbers and - unfortunately - a suctraction sign.
``````var saleItems=sales[i].toString();
trace(saleItems) results in the below
//
1
-
-
3
-
-
1
-
1
-
-
-
3
-
-
-
-
-
1
2
2
-
1
1
-
9
``````

How do I add the numbers and how do I remove the subtraction-signs.
###### Who is Participating?

Commented:
``````var validChars:String = "0123456789";
var saleItems:Array = ["1", "2", "+", "-", "3", "4", "1c", "10"];
for(var i:Number = saleItems.length - 1; i >= 0; i--){
var item:String = saleItems[i];
for(var c:Number = 0; c < item.length; c++){
if(validChars.indexOf(item.charAt(c)) == -1){
saleItems.splice(i, 1);
break;
}
}
}
trace(saleItems)

//Another solution working for ALL number, but you cannot controll for example skip number 5 or 6
var saleItems:Array = ["1", "2", "+", "-", "3", "4", "1c", "10"];
for(var i:Number = saleItems.length - 1; i >= 0; i--){
if(isNaN(new Number(saleItems[i])))
saleItems.splice(i, 1);
}

trace(saleItems)
``````
0

Commented:
``````var validChars:String = "0123456789";
var saleItems:Array = ["1", "2", "+", "-", "3", "4"];
for(var i:Number = saleItems.length - 1; i >= 0; i--){
if(validChars.indexOf(saleItems[i]) == -1)
saleItems.splice(i, 1);
}
trace(saleItems)
``````
0

Author Commented:
Thanks, that seems to work, but how do I get a sum of the numbers. In your example the sum 10.
0

Commented:
You can use string.length and charAt function for substring and compare each symbol in your area item. I cannot code for now but is
Loop inside loop. Try to write code if not I will submit my version an in hour

0

Author Commented:
Thanks, they works fine, but I did a try myself and ended up with this solution.

``````var total:Array=[];
var sum:Number=0;
var salesItems:Array=["1", "2", "+", "-", "3", "4", "1c", "10"];
for each(var o:Number in salesItems){
if(!o){
o=0;
}
total.push(o);
}
for (var i:Number = 0; i < total.length; i++){
sum+=total[i];
}
trace(sum);      // 20
``````

It seems to work.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.