arr.reduce((sum, item) => sum + item.age, 0) / arr.length
в алерте просто вызов функции сделать alert(getAverageAge(arr))
const getAverageAge = a => {
let sum = [];
for (i of a){
sum.push(i.age)
}
return sum.reduce((function (acc,i) { return acc + i }),0) / sum.length;
}
let v = {name: 'Vasia', age: 25}
let p = {name: 'Petia', age: 30}
let m = {name: 'Masha', age: 29}
let arr = [v, p, m]
alert(getAverageAge(arr))
Но вообще reduce тут излишен const getAverageAge = a => {
let sum = 0;
for (var i = 0; i < a.length; i++) {
sum += a[i].age
}
return sum / i
}
let v = {name: 'Vasia', age: 25}
let p = {name: 'Petia', age: 30}
let m = {name: 'Masha', age: 29}
let arr = [v, p, m]
alert(getAverageAge(arr))
const getAverageAge = a => {
let sum = 0;
for (var i = 0; i < a.length; i++) {
sum += a[i].age
}
return sum / i
}
let v = {name: 'Vasia', age: 25}
let p = {name: 'Petia', age: 30}
let m = {name: 'Masha', age: 29}
let arr = [v, p, m]
alert(getAverageAge(arr))
И вообще, правильно ли прога сделана, может там дело вообще не в выводе?