Skip to content

Commit 6d357fa

Browse files
committed
Add solution to getTheTitles and findTheOldest exercises
1 parent c2d35be commit 6d357fa

3 files changed

Lines changed: 74 additions & 67 deletions

File tree

findTheOldest/findTheOldest.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
let findTheOldest = function() {
1+
let findTheOldest = function (arr) {
2+
const today = new Date();
3+
const currYear = today.getFullYear();
4+
arr.sort((a, b) => {
5+
return (
6+
(b.yearOfDeath || currYear) - b.yearOfBirth - ((a.yearOfDeath || currYear) - a.yearOfBirth)
7+
);
8+
});
29

3-
}
10+
return arr[0];
11+
};
412

5-
module.exports = findTheOldest
13+
module.exports = findTheOldest;
Lines changed: 60 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
1-
let findTheOldest = require('./findTheOldest')
2-
3-
describe('findTheOldest', function() {
4-
it('finds the oldest person!', function() {
5-
const people = [
6-
{
7-
name: 'Carly',
8-
yearOfBirth: 1942,
9-
yearOfDeath: 1970,
10-
},
11-
{
12-
name: 'Ray',
13-
yearOfBirth: 1962,
14-
yearOfDeath: 2011
15-
},
16-
{
17-
name: 'Jane',
18-
yearOfBirth: 1912,
19-
yearOfDeath: 1941
20-
},
21-
]
22-
expect(findTheOldest(people).name).toEqual('Ray');
23-
});
24-
xit('finds the oldest person if someone is still living', function() {
25-
const people = [
26-
{
27-
name: 'Carly',
28-
yearOfBirth: 2018,
29-
},
30-
{
31-
name: 'Ray',
32-
yearOfBirth: 1962,
33-
yearOfDeath: 2011
34-
},
35-
{
36-
name: 'Jane',
37-
yearOfBirth: 1912,
38-
yearOfDeath: 1941
39-
},
40-
]
41-
expect(findTheOldest(people).name).toEqual('Ray');
42-
});
43-
xit('finds the oldest person if the OLDEST is still living', function() {
44-
const people = [
45-
{
46-
name: 'Carly',
47-
yearOfBirth: 1066,
48-
},
49-
{
50-
name: 'Ray',
51-
yearOfBirth: 1962,
52-
yearOfDeath: 2011
53-
},
54-
{
55-
name: 'Jane',
56-
yearOfBirth: 1912,
57-
yearOfDeath: 1941
58-
},
59-
]
60-
expect(findTheOldest(people).name).toEqual('Carly');
61-
});
1+
let findTheOldest = require("./findTheOldest");
622

3+
describe("findTheOldest", function () {
4+
it("finds the oldest person!", function () {
5+
const people = [
6+
{
7+
name: "Carly",
8+
yearOfBirth: 1942,
9+
yearOfDeath: 1970,
10+
},
11+
{
12+
name: "Ray",
13+
yearOfBirth: 1962,
14+
yearOfDeath: 2011,
15+
},
16+
{
17+
name: "Jane",
18+
yearOfBirth: 1912,
19+
yearOfDeath: 1941,
20+
},
21+
];
22+
expect(findTheOldest(people).name).toEqual("Ray");
23+
});
24+
it("finds the oldest person if someone is still living", function () {
25+
const people = [
26+
{
27+
name: "Carly",
28+
yearOfBirth: 2018,
29+
},
30+
{
31+
name: "Ray",
32+
yearOfBirth: 1962,
33+
yearOfDeath: 2011,
34+
},
35+
{
36+
name: "Jane",
37+
yearOfBirth: 1912,
38+
yearOfDeath: 1941,
39+
},
40+
];
41+
expect(findTheOldest(people).name).toEqual("Ray");
42+
});
43+
it("finds the oldest person if the OLDEST is still living", function () {
44+
const people = [
45+
{
46+
name: "Carly",
47+
yearOfBirth: 1066,
48+
},
49+
{
50+
name: "Ray",
51+
yearOfBirth: 1962,
52+
yearOfDeath: 2011,
53+
},
54+
{
55+
name: "Jane",
56+
yearOfBirth: 1912,
57+
yearOfDeath: 1941,
58+
},
59+
];
60+
expect(findTheOldest(people).name).toEqual("Carly");
61+
});
6362
});

getTheTitles/getTheTitles.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const getTheTitles = function() {
2-
3-
}
1+
const getTheTitles = function (arr) {
2+
return arr.map((x) => x.title);
3+
};
44

55
module.exports = getTheTitles;

0 commit comments

Comments
 (0)