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 age, this is what I have 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 get when I run this function, Not a number
But I can work:
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 is that I took + = and just made it =, and I removed the use of our variable and I just gave it the first index of the array. Therefore, at the same time, I know that the function correctly replaces the array of arrayPeople parameters, but there is something wrong in my for loop that I stop from 0 and 1 (arrayPeople indices).
PS I'm trying to do this on my own, and I'm not looking for someone to just make this problem for me, I would really appreciate some hints on what to fix or maybe I need to explore how to solve this problem .
Thanks!!
source share