Dapatkan rata-rata umur
Tulis fungsi getAverageAge(users)
yang menerima sebuah array dari objek dengan properti age
dan mengembalikan rata-rata umur.
Rumus dari rata-rata adalah (age1 + age2 + ... + ageN) / N
.
Contoh:
let john = { name: "John", age: 25 };
let pete = { name: "Pete", age: 30 };
let mary = { name: "Mary", age: 29 };
let arr = [ john, pete, mary ];
alert( getAverageAge(arr) ); // (25 + 30 + 29) / 3 = 28
function getAverageAge(users) {
return users.reduce((prev, user) => prev + user.age, 0) / users.length;
}
let john = { name: "John", age: 25 };
let pete = { name: "Pete", age: 30 };
let mary = { name: "Mary", age: 29 };
let arr = [ john, pete, mary ];
alert( getAverageAge(arr) ); // 28