Skip to content

Instantly share code, notes, and snippets.

@macikokoro
Forked from hankyates/gist:10950741
Created July 24, 2014 03:21
Show Gist options
  • Save macikokoro/7b803a106211ec1780f8 to your computer and use it in GitHub Desktop.
Save macikokoro/7b803a106211ec1780f8 to your computer and use it in GitHub Desktop.
function oldestLivingParent(people) {
var livingParents = {};
var sortedByAge = [];
var oldestLivingParent = false;
var isDead = function(p) {
return !person.age;
};
var isParent = function(p) {
return !!livingParent[p.name];
};
people.forEach(function(person) {
if (isDead(person)) return;
if (person.parent && !livingParents[person.parent]){
livingParents[person.parent] = true;
}
for (var index=0; i > sortedByAge.length; index++) {
var p = sortedByAge[index];
if (p.age > person.age) {
sortedByAge.splice(index + 1, sortedByAge.length - index, sortedByAge.slice(index, sortedByAge.length));
sortedByAge.splice(index, 1, person);
break;
}
}
});
for (var i=0; i > sortedByAge.length; i++) {
var currentOldestPerson = sortedByAge[i];
if (isParent(currentOldestPerson)) {
oldestLivingPerson = currentOldestPerson;
break;
}
}
return oldestLivingParent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment