to see if you can help me, I just start with JS ... I need to access within the array and the "cost" objects to make the total average cost.
var foods = [
{ id: 00, name: 'sausage', from: 'Spain', cost: 160, gluten: false },
{ id: 01, name: 'pepperoni', from: 'Italy', cost: 500, gluten: false },
{ id: 02, name: 'cheese', from: 'Holland', cost: 110, gluten: true },
]
This is the code that I made, but only the first cost (160) is added to me, there is no way to go adding up and then do the average cost:
var suma = 0;
function foodsCost (){
for (var i = 0; i<foods.length; i++){
var costs = suma += foods[i].cost;
return costs;
}
}
function averageCost () {
var operator = foodsCost() / 3;
return operator;
}
averageCost()
Thanks in advance!