Skip to content

Commit

Permalink
✔️ exercise findTheOldest
Browse files Browse the repository at this point in the history
  • Loading branch information
sameem420 committed Mar 12, 2024
1 parent fd9baf0 commit 6e12c24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
17 changes: 15 additions & 2 deletions JavaScript/javascript-exercises/12_findTheOldest/findTheOldest.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const findTheOldest = function() {

const findTheOldest = function (array) {
return array.reduce((oldest, currentPerson) => {
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
const currentAge = getAge(
currentPerson.yearOfBirth,
currentPerson.yearOfDeath
);
return oldestAge < currentAge ? currentPerson : oldest;
});
};

const getAge = function (birth, death) {
if (!death) {
death = new Date().getFullYear();
}
return death - birth;
};
// Do not edit below this line
module.exports = findTheOldest;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const findTheOldest = require('./findTheOldest')
const findTheOldest = require("./findTheOldest");

describe('findTheOldest', () => {
test('finds the person with the greatest age!', () => {
describe("findTheOldest", () => {
test("finds the person with the greatest age!", () => {
const people = [
{
name: "Carly",
Expand All @@ -18,10 +18,10 @@ describe('findTheOldest', () => {
yearOfBirth: 1912,
yearOfDeath: 1941,
},
]
expect(findTheOldest(people).name).toBe('Ray');
];
expect(findTheOldest(people).name).toBe("Ray");
});
test.skip('finds the oldest person if yearOfDeath field is undefined on a non-oldest person', () => {
test("finds the oldest person if yearOfDeath field is undefined on a non-oldest person", () => {
const people = [
{
name: "Carly",
Expand All @@ -37,10 +37,10 @@ describe('findTheOldest', () => {
yearOfBirth: 1912,
yearOfDeath: 1941,
},
]
expect(findTheOldest(people).name).toBe('Ray');
];
expect(findTheOldest(people).name).toBe("Ray");
});
test.skip('finds the oldest person if yearOfDeath field is undefined for the oldest person', () => {
test("finds the oldest person if yearOfDeath field is undefined for the oldest person", () => {
const people = [
{
name: "Carly",
Expand All @@ -56,7 +56,7 @@ describe('findTheOldest', () => {
yearOfBirth: 1912,
yearOfDeath: 1941,
},
]
expect(findTheOldest(people).name).toBe('Carly');
];
expect(findTheOldest(people).name).toBe("Carly");
});
});

0 comments on commit 6e12c24

Please sign in to comment.