8000 Complete exercise 12 · josh-Fen/javascript-exercises@21de113 · GitHub
[go: up one dir, main page]

Skip to content

Commit 21de113

Browse files
committed
Complete exercise 12
1 parent 5309650 commit 21de113

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

12_findTheOldest/findTheOldest.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
const findTheOldest = function() {
1+
const findTheOldest = function (arr) {
2+
return arr.reduce((obj, element) => {
23

4+
if (!obj.yearOfBirth) {
5+
return element;
6+
}
7+
8+
const birth = element.yearOfBirth;
9+
const death = element.yearOfDeath ? element.yearOfDeath : new Date().getFullYear();
10+
const yearsAlive = death - birth;
11+
12+
const objBirth = obj.yearOfBirth;
13+
const objDeath = obj.yearOfDeath ? obj.yearOfDeath : new Date().getFullYear();
14+
const objYearsAlive = objDeath - objBirth;
15+
16+
return yearsAlive > objYearsAlive ? element : obj;
17+
}, {});
318
};
419

520
// Do not edit below this line

12_findTheOldest/findTheOldest.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('findTheOldest', () => {
2121
]
2222
expect(findTheOldest(people).name).toBe('Ray');
2323
});
24-
test.skip('finds the oldest person if yearOfDeath field is undefined on a non-oldest person', () => {
24+
test('finds the oldest person if yearOfDeath field is undefined on a non-oldest person', () => {
2525
const people = [
2626
{
2727
name: "Carly",
@@ -40,7 +40,7 @@ describe('findTheOldest', () => {
4040
]
4141
expect(findTheOldest(people).name).toBe('Ray');
4242
});
43-
test.skip('finds the oldest person if yearOfDeath field is undefined for the oldest person', () => {
43+
test('finds the oldest person if yearOfDeath field is undefined for the oldest person', () => {
4444
const people = [
4545
{
4646
name: "Carly",

0 commit comments

Comments
 (0)
0