sum of array works with 1 array not another
Hello all,
Once again I call on the gurus after banging my head against the wall for a few hours. I use .clone to clone some elements then I get the various values in an array. All that works fine. However, I want to total up the array so I can validate the total against another value.
In the code below if I put the x array in the sum such as sum(x) and alert(quantityTotal) it shows 4 as expected. If I simply alert(quantities) I get 3,1 however, if I sum(quantities) and alert(quantityTotal) I get 013 instead of the expected total of 4 I'm baffled (as usual..haha). Must be something simple I'm missing.
Any help would be appreciated.
Thanks in advance,
Twitch
- var quantities = [];//quantities array
- $.each($('.quantity'), function() {
- quantities.push($(this).val());
- });
-
- sum = function(o){//function to sum an array
- for(var s = 0, i = o.length; i; s += o[--i]);
- return s;
- };
- var x = [3,1];
- var quantityTotal = sum(x);//set quantityTotal to the sum of the array
- alert(quantityTotal);