8000 Pass all tests · MonsWriter/javascript-exercises@e0e8935 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0e8935

Browse files
committed
Pass all tests
1 parent 366224b commit e0e8935

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

12_findTheOldest/findTheOldest.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
const findTheOldest = function() {
2-
3-
};
1+
const findTheOldest = function (array) {
2+
return array.reduce((oldest, currentPerson) => {
3+
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
4+
const currentAge = getAge(
5+
currentPerson.yearOfBirth,
6+
currentPerson.yearOfDeath
7+
);
8+
return oldestAge < currentAge ? currentPerson : oldest;
9+
});
10+
};
11+
12+
const getAge = function (birth, death) {
13+
if (!death) {
14+
death = new Date().getFullYear();
15+
}
16+
return death - birth;
17+
};
418

519
// Do not edit below this line
620
module.exports = findTheOldest;

0 commit comments

Comments
 (0)
0