Update findTheOldest.js

Added option using sort method
This commit is contained in:
Dovid Majowka 2020-07-03 00:34:17 +03:00 committed by GitHub
parent 3b51e5dbef
commit f22399369c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 0 deletions

View File

@ -1,3 +1,4 @@
// using reduce method
const findTheOldest = function(array) { const findTheOldest = function(array) {
return array.reduce((oldest, currentPerson) => { return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath) const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath)
@ -14,3 +15,20 @@ const getAge = function(birth, death) {
} }
module.exports = findTheOldest module.exports = findTheOldest
// option using sort method
// let findTheOldest = function(people) {
// const sorted = people.sort((a, b) => {
// if(b.yearOfDeath === undefined && b.yearOfBirth < a.yearOfBirth){
// return 1
// }
// if((a.yearOfDeath - a.yearOfBirth) < (b.yearOfDeath - b.yearOfBirth)){
// return 1
// }
// else{
// return -1
// }
// })
// return sorted[0]
// }