I have an array of users with ages, I want to get the average age of users. So far I tried to do it with reduce
but it won't implement it's not the right syntax for reduce
.
Here is my code:
let sam = { name: "Sam", age: 21 };
let hannah = { name: "Hannah", age: 33 };
let alex = { name: "Alex", age: 24 };
let users = [ sam, hannah, alex ];
function getAverageAge(array){
let sumAge = array.age.reduce(function(sum, current) {
return sum + current;
}, 0)
return (sumAge / (array.length + 1));
}
console.log( getAverageAge(users) ); // 21 + 33 + 24 / 3 = 26
In this case, it should return 26.