8000 Finished findTheOldest with array.reduce function · xinding33/javascript-exercises@f2589e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f2589e6

Browse files
committed
Finished findTheOldest with array.reduce function
1 parent 1ad25dd commit f2589e6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

findTheOldest/findTheOldest.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
let findTheOldest = function() {
1+
let findTheOldest = function(people) {
2+
return people.reduce((oldestPerson, currentPerson) => {
3+
let oldestAge = getAge(oldestPerson);
4+
let currentAge = getAge(currentPerson);
5+
return oldestAge > currentAge ? oldestPerson : currentPerson;
6+
})
7+
}
28

9+
let getAge = function(person) {
10+
return person.yearOfDeath == null ? new Date().getFullYear() - person.yearOfBirth : person.yearOfDeath - person.yearOfBirth;
311
}
412

513
module.exports = findTheOldest

findTheOldest/findTheOldest.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('findTheOldest', function() {
2121
]
2222
expect(findTheOldest(people).name).toEqual('Ray');
2323
});
24-
xit('finds the oldest person if someone is still living', function() {
24+
it('finds the oldest person if someone is still living', function() {
2525
const people = [
2626
{
2727
name: 'Carly',
@@ -40,7 +40,7 @@ describe('findTheOldest', function() {
4040
]
4141
expect(findTheOldest(people).name).toEqual('Ray');
4242
});
43-
xit('finds the oldest person if the OLDEST is still living', function() {
43+
it('finds the oldest person if the OLDEST is still living', function() {
4444
const people = [
4545
{
4646
name: 'Carly',

0 commit comments

Comments
 (0)
0