tags:

views:

133

answers:

2

I have this code:

var totalAmt=0;
for (i in orders)
{
   order=orders[i];
   if (order.status !='Cancelled')
        totalAmt=totalAmt + order.amount;
}

But if i have 3 orders with amounts 3, 1, and 5, then instead of totalAmt being 9, i get 0315. So I think its adding the amounts together as strings instead of integars.

How do I fix this?

+9  A: 

order.amount is a string, and if one of the operands of the + operator is a string, a concatenation is done instead of a sum.

You should convert it to number, for example using the unary plus operator:

var totalAmt = 0, i, order; // declaring the loop variables
for (var i in orders) {
   order = orders[i];
   if (order.status !='Cancelled')
        totalAmt += +order.amount; // unary plus to convert to number
}

You could alternatively use:

totalAmt = totalAmt + (+order.amount);
totalAmt = totalAmt + Number(order.amount);
totalAmt = totalAmt + parseFloat(order.amount);
// etc...

Also you are using a for..in loop to iterate over orders, if orders is an array, you should use a normal for loop:

for (var i = 0; i<orders.length; i++) {
  //...
}

That is because the for...in statement is intended to be used for iterate over object properties, for arrays it may be tempting to use it, because seems to work, but it's not recommended since it will iterate over the object properties, and if you have extended the Array.prototype, those properties will be also iterated in addition to the numeric indexes.

Another reason to avoid it is because the order of iteration used by this statement is arbitrary, and iterating over an array may not visit elements in numeric order, and also it seems to be much more slower than a simple for loop.

If iteration order is not important, I personally like iterating backwards:

var i = orders.length;
while (i--) {
   //...
}
CMS
this is what i wanted. why in the heck would it convert `order.amount` to string though?
Click Upvote
Why should i use a `for` to loop it over when `for in` is easier? Isn't it just personal preference or is there any real advantage of `for`?
Click Upvote
@Click: its hard to tell if the creation of the order object is not posted here. I garner that the property amount is created as a string, via json, like {amount:"30", ...}, and that will cause this problem
Chii
@Click: The for...in statement is intended to be used for iterate over object properties (http://is.gd/3cxtp), for arrays works but its not recommended since it will iterate over properties, if you have extended the Array.prototype, those properties will be also iterated, and it seems to be much more slower than a simple for loop (http://is.gd/3cxPl) ...
CMS
@Click: `"for..in"` crawls up the `Array.prototype` chain. See http://www.ecma-international.org/publications/standards/Ecma-262.htm (Sec 12.6.4), while a standard `for` loop does not. Also, the order of items iterated with `"for..in"` is not guaranteed. A standard `for` loop is.
Crescent Fresh
If you need to use order.amount for other things, I recommend that you first type this: order.amount -= 0; because the subtraction by 0 yields a number. Then order.amount will be a number and ready to use for whatever arithmetic you wish to do.
Robert L
A String might be in use for numbers if there are amounts of money involved. JavaScript Number is a floating point number and so unsuitable for fractional money.
bobince
+1  A: 

Use the parseFloat() or parseInt() functions to convert the string to the appropriate type.

rmarimon