sum of array works with 1 array not another

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

  1.  var quantities = [];//quantities array
  2.     $.each($('.quantity'), function() {
  3.         quantities.push($(this).val());
  4.     });
  5. sum = function(o){//function to sum an array
  6.     for(var s = 0, i = o.length; i; s += o[--i]);
  7.     return s;
  8. };

  9. var x = [3,1];
  10.     var quantityTotal = sum(x);//set quantityTotal to the sum of the array

  11. alert(quantityTotal);