Reputation: 375
I have an array with properties, take the following for example:
var arrayPeople = [
{
name: 'joe',
job: 'programmer',
age: 25,
},
{
name: 'bob',
job: 'plumber',
age: 30,
};
];
I want to create a function that will return their average ages, here is what i have that is currently not working
var ageAverage = function(array) {
var age = 0, average;
for (var i = 0; i < array.length; i++) {
age += array[i].age;
}
average = age / array.length;
return average;
};
What I am getting when running this function is Not A Number
But what I can get to work is:
var ageAverage = function(array) {
var age = 0, average;
for (var i = 0; i < array.length; i++) {
age = array[0].age;
}
average = age / array.length;
return average;
};
The subtle difference here is that I took away the += and just made it =, and i took away the use of our variable i and just gave it the first index of the array. So in doing this, I know that the function is correctly substituting the parameter array with arrayPeople, but there is something going wrong in my for loop that is stopping i from becoming 0 and 1 (the indexes of arrayPeople).
P.S. I am trying to do this on my own, and I am not looking for someone to just do the problem for me, I would really appreciate just some hints on what to fix or maybe a concept I need to research the understand how to fix this problem.
Thanks!!
Upvotes: 11
Views: 26036
Reputation: 26771
There's always a one-liner.
arrayPeople.reduce((a, o, i, p) => a + o.age / p.length, 0));
console.log(`${[{
name: 'joe',
job: 'programmer',
age: 25,
},
{
name: 'bob',
job: 'plumber',
age: 30,
}
].reduce((a, o, i, p) => a + o.age / p.length, 0)}%`);
Upvotes: 3
Reputation: 77482
You need initialize age
variable (var age = 0
)
var arrayPeople = [{
name: 'joe',
job: 'programmer',
age: 25,
}, {
name: 'bob',
job: 'plumber',
age: 30,
}];
var ageAverage = function(array) {
var age = 0,
average;
for (var i = 0; i < array.length; i++) {
age += array[i].age;
}
average = age / array.length;
return average;
};
console.log(
ageAverage(arrayPeople)
);
in your variant, age
variable equals undefined
, and undefined += number
(for example 25) returns NaN
;
also change ,
to ;
in this line
from
for (var i = 0, i < array.length; i++) {
to
for (var i = 0; i < array.length; i++) {
Upvotes: 5
Reputation: 74146
Consider using map
and reduce
:
arrayPeople.
map(function(item){ return item.age; }).
reduce(function(a, b){ return a + b; }, 0) / arrayPeople.length;
Or just the reduce
as @KooiInc pointed out:
arrayPeople.reduce(function (a,b) { return a + b.age; }, 0) / arrayPeople.length;
A working Example:
var arrayPeople = [{
name: 'joe',
job: 'programmer',
age: 25,
}, {
name: 'bob',
job: 'plumber',
age: 30,
}],
average = arrayPeople.reduce(function(a, b) {
return a + b.age;
}, 0) / arrayPeople.length;
document.write(average);
Upvotes: 34
Reputation: 370
You should change the for to:
for (var i = 0; i < array.length; i++) instead of for (var i = 0, i < array.length; i++)
and give initial value to age
var age=0
Upvotes: 2