File tree Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Expand file tree Collapse file tree 2 files changed +18
-3
lines changed Original file line number Diff line number Diff line change 1
- const findTheOldest = function ( ) {
1
+ const findTheOldest = function ( arr ) {
2
+ return arr . reduce ( ( obj , element ) => {
2
3
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
+ } , { } ) ;
3
18
} ;
4
19
5
20
// Do not edit below this line
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ describe('findTheOldest', () => {
21
21
]
22
22
expect ( findTheOldest ( people ) . name ) . toBe ( 'Ray' ) ;
23
23
} ) ;
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' , ( ) => {
25
25
const people = [
26
26
{
27
27
name : "Carly" ,
@@ -40,7 +40,7 @@ describe('findTheOldest', () => {
40
40
]
41
41
expect ( findTheOldest ( people ) . name ) . toBe ( 'Ray' ) ;
42
42
} ) ;
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' , ( ) => {
44
44
const people = [
45
45
{
46
46
name : "Carly" ,
You can’t perform that action at this time.
0 commit comments