JSFiddle - React, Tailwind, and code Playground
by fairmutex
HTML
<script src="http://eloquentjavascript.net/code/ancestry.js"></script>
JavaScript
var ancestry = JSON.parse(ANCESTRY_FILE);
//console.log(ancestry);
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;
});
// remove all that dont have a mother and those which mother is not in the list as we dont know their birth year
var ancestry_with_mum = ancestry.filter(function(c,i,a){
// return (c.mother != null && byName[c.mother] !== undefined);
return byName[c.mother] !== undefined;
});
// calculate difference of child birth and mum birth
var diff = ancestry_with_mum.map(function(c,i,a){
return Number(c.born) - Number(byName[c.mother].born)
});
console.log( ancestry_with_mum );
console.log( diff );
console.log(average(diff));