jQuery jAM, Season 6

Exercise 5.2

Mother_child Difference

	 
	 
var ancestry = JSON.parse(ANCESTRY_FILE);
console.log(ancestry.length);


function average(array) {
  function plus(a, b) 
  	{ 
  		return a + b; 
  		}
  return array.reduce(plus) / array.length;
}

var byName = {};
ancestry.forEach(function(person) {
  byName[person.name] = person;
});

var differences = ancestry.filter(function(person) {
  return byName[person.mother] != null;
}).map(function(person) {
  return person.born - byName[person.mother].born;
});

console.log(average(differences)); 

	 
	

To open the JavaScript console, press F12 or on MAC press COMMAND-OPTION-I.