mirror of
https://github.com/10h30/odin-javascript-exercises.git
synced 2026-06-05 15:09:09 +09:00
Completed excercise 12
This commit is contained in:
@@ -1,5 +1,30 @@
|
|||||||
const findTheOldest = function() {
|
const findTheOldest = function(people) {
|
||||||
|
console.log(people)
|
||||||
|
const sortByAge = people.sort((a,b) => {
|
||||||
|
|
||||||
|
if (!a.yearOfDeath) {
|
||||||
|
let currentYear = new Date().getFullYear(); console.log(currentYear)
|
||||||
|
ageOfa = currentYear - a.yearOfBirth
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ageOfa = a.yearOfDeath - a.yearOfBirth
|
||||||
|
}
|
||||||
|
console.log(a.name + ": " + ageOfa)
|
||||||
|
|
||||||
|
if (!b.yearOfDeath) {
|
||||||
|
let currentYear = new Date().getFullYear();
|
||||||
|
ageOfb = currentYear - b.yearOfBirth
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
ageOfb = b.yearOfDeath - b.yearOfBirth
|
||||||
|
}
|
||||||
|
console.log(b.name + ": " + ageOfb)
|
||||||
|
|
||||||
|
return ageOfa < ageOfb ? 1 : -1
|
||||||
|
})
|
||||||
|
const oldest = sortByAge[0]
|
||||||
|
console.log(oldest);
|
||||||
|
return oldest;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Do not edit below this line
|
// Do not edit below this line
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ describe('findTheOldest', () => {
|
|||||||
]
|
]
|
||||||
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 = [
|
const people = [
|
||||||
{
|
{
|
||||||
name: "Carly",
|
name: "Carly",
|
||||||
@@ -40,7 +40,7 @@ describe('findTheOldest', () => {
|
|||||||
]
|
]
|
||||||
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 = [
|
const people = [
|
||||||
{
|
{
|
||||||
name: "Carly",
|
name: "Carly",
|
||||||
|
|||||||
Reference in New Issue
Block a user