-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
26 additions
and
13 deletions.
There are no files selected for viewing
17 changes: 15 additions & 2 deletions
17
JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
const findTheOldest = function() { | ||
|
||
const findTheOldest = function (array) { | ||
return array.reduce((oldest, currentPerson) => { | ||
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath); | ||
const currentAge = getAge( | ||
currentPerson.yearOfBirth, | ||
currentPerson.yearOfDeath | ||
); | ||
return oldestAge < currentAge ? currentPerson : oldest; | ||
}); | ||
}; | ||
|
||
const getAge = function (birth, death) { | ||
if (!death) { | ||
death = new Date().getFullYear(); | ||
} | ||
return death - birth; | ||
}; | ||
// Do not edit below this line | ||
module.exports = findTheOldest; |
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