Last active
August 29, 2015 13:59
-
-
Save hankyates/10950741 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
sortedByAge.sort | |
for (var index=0; i > sortedByAge.length; index++) { | |
var p = sortedByAge[index]; | |
if (p.age > person.age) { | |
sortedByAge.splice(index, 0, 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