8000 Added solution to findTheOldest · Am-Scan/javascript-exercises@9c34088 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9c34088

Browse files
committed
Added solution to findTheOldest
1 parent a6ed0f8 commit 9c34088

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

findTheOldest/findTheOldest.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
let findTheOldest = function() {
1+
function getAge(dob,dod) {
2+
if (dod === undefined) {
3+
let now = new Date();
4+
dod = now.getFullYear();
5+
}
6+
let age = dod - dob;
7+
return age;
8+
}
29

10+
let findTheOldest = function(array) {
11+
return array.reduce(function (currentOldest, currentPerson) {
12+
const oldestAge = getAge(currentOldest.yearOfBirth,currentOldest.yearOfDeath);
13+
const currentAge = getAge(currentPerson.yearOfBirth,currentPerson.yearOfDeath);
14+
if (currentAge > oldestAge) {
15+
return currentPerson;
16+
}
17+
else {
18+
return currentOldest;
19+
}
20+
});
321
}
422

523
module.exports = findTheOldest

findTheOldest/findTheOldest.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
let findTheOldest = require('./findTheOldest')
22

33
describe('findTheOldest', function() {
4-
it('finds the oldest person!', function() {
4+
xit('finds the oldest person!', function() {
55
const people = [
66
{
77
name: 'Carly',
@@ -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