add findOldest Solutions
This commit is contained in:
parent
c0ef28a362
commit
ffa00275cc
3 changed files with 90 additions and 0 deletions
10
findTheOldest/README.md
Normal file
10
findTheOldest/README.md
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# Find the Oldest
|
||||||
|
|
||||||
|
given an array of objects representing people with a birth and death year, return the oldest person.
|
||||||
|
|
||||||
|
## Hints
|
||||||
|
- You should return the whole person object, but the tests mostly just check to make sure the name is correct.
|
||||||
|
- this can be done with a couple of chained array methods, or by using `reduce`.
|
||||||
|
- One of the tests checks for people with no death-date.. use JavaScript's Date function to get their age as of today.
|
||||||
|
|
||||||
|
|
16
findTheOldest/findTheOldest.js
Normal file
16
findTheOldest/findTheOldest.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = findTheOldest
|
64
findTheOldest/findTheOldest.spec.js
Normal file
64
findTheOldest/findTheOldest.spec.js
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
let findTheOldest = require('./findTheOldest')
|
||||||
|
|
||||||
|
describe('findTheOldest', function() {
|
||||||
|
it('finds the oldest person!', function() {
|
||||||
|
const people = [
|
||||||
|
{
|
||||||
|
name: 'Carly',
|
||||||
|
yearOfBirth: 1942,
|
||||||
|
yearOfDeath: 1970,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Ray',
|
||||||
|
yearOfBirth: 1962,
|
||||||
|
yearOfDeath: 2011
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Jane',
|
||||||
|
yearOfBirth: 1912,
|
||||||
|
yearOfDeath: 1941
|
||||||
|
},
|
||||||
|
]
|
||||||
|
expect(findTheOldest(people).name).toEqual('Ray');
|
||||||
|
});
|
||||||
|
it('finds the oldest person if someone is still living', function() {
|
||||||
|
const people = [
|
||||||
|
{
|
||||||
|
name: 'Carly',
|
||||||
|
yearOfBirth: 2018,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Ray',
|
||||||
|
yearOfBirth: 1962,
|
||||||
|
yearOfDeath: 2011
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Jane',
|
||||||
|
yearOfBirth: 1912,
|
||||||
|
yearOfDeath: 1941
|
||||||
|
},
|
||||||
|
]
|
||||||
|
expect(findTheOldest(people).name).toEqual('Ray');
|
||||||
|
});
|
||||||
|
it('finds the oldest person if the OLDEST is still living', function() {
|
||||||
|
const people = [
|
||||||
|
{
|
||||||
|
name: 'Carly',
|
||||||
|
yearOfBirth: 1066,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Ray',
|
||||||
|
yearOfBirth: 1962,
|
||||||
|
yearOfDeath: 2011
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Jane',
|
||||||
|
yearOfBirth: 1912,
|
||||||
|
yearOfDeath: 1941
|
||||||
|
},
|
||||||
|
]
|
||||||
|
expect(findTheOldest(people).name).toEqual('Carly');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in a new issue