8000 fiished findTheOldest exercise · unconnect/javascript-exercises@468001f · GitHub
[go: up one dir, main page]

Skip to content

Commit 468001f

Browse files
committed
fiished findTheOldest exercise
1 parent 0c50bc1 commit 468001f

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

12_findTheOldest/findTheOldest.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
const findTheOldest = function() {
2-
1+
const findTheOldest = function(people = {}) {
2+
return people.reduce((prevPerson, currentPerson) => {
3+
if (currentPerson["yearOfDeath"] === undefined)
4+
currentPerson["yearOfDeath"] = new Date().getFullYear();
5+
if (prevPerson.name === undefined) return currentPerson
6+
if(prevPerson.yearOfDeath - prevPerson.yearOfBirth <
7+
currentPerson.yearOfDeath -
8+
currentPerson.yearOfBirth) prevPerson = currentPerson;
9+
return prevPerson
10+
}, {})
311
};
412

513
// 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 someone is still living', () => {
24+
test('finds the oldest person if someone is still living', () => {
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 the OLDEST is still living', () => {
43+
test('finds the oldest person if the OLDEST is still living', () => {
4444
const people = [
4545
{
4646
name: "Carly",

0 commit comments

Comments
 (0)
0