Adding array values together

So, i’m being a fish out of water again…yey.

I built a simple array :

var itemList = new Item[40];

class Item {
    var itemName : String;
    var sellPrice : int;
    var weight : float;
    var quantityInInventory : int;
}

And then set about populating the list. Now for the dillemma. How do i add all the weights of each item together then display the total?

I am using javascript.

Hi,

The code in C# looks like that:

float	total = 0;
for (int i = 0; i < items.Length; i++)
{
	total += items*.weight;*

}
return total;
In JS:
class Item
{
var weight : float;
}
var items : Item[];
function GetTotal()
{
var total = 0.0f;
for (var i = 0; i < items.Length; i++)
{
_ total += items*.weight;_
_
}_
return total;
_
}*_

You don’t have a list or array in this code snippet but I guess you have a list of these items like so:

var items : List.<Item>();

Then you add items with for example:

items.Add(someItem);

You calculate the total weight with:

float TotalWeightOfItems() {
   float totalWeight = 0;
   foreach (item in items) {
      totalWeight += item.weight;
   }
   return totalWeight;
}

var itemList = new Item[40];
class Item {
var itemName : String;
var sellPrice : int;
var weight : float;
var quantityInInventory : int;
}

 // set some values
itemList[2].sellPrice=23;
itemList[5].sellPrice=18;
itemList[8].sellPrice=3;

var total : int=0;
for(var it : Item in itemList){
total+=it.sellPrice;}

print(total);