8000 Exercise 12: Provide alternative solution (#459) · mstottrop/javascript-exercises@1695abe · GitHub
[go: up one dir, main page]

Skip to content

Commit 1695abe

Browse files
nik-revMaoShizhong
andauthored
Exercise 12: Provide alternative solution (TheOdinProject#459)
* feat(12): improve solution by making it more cleaner * refactor(12): rename `array` to `people` * feat(12): improve explanation of `??=` Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> * feat(12): include both solutions * feat(12): hint to use various array methods * refactor(12): add semicolon Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> * feat(12): use block comments instead of line for readability Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> * refactor(12): remove unnecessary comment Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --------- Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com>
1 parent 45a5ced commit 1695abe

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

12_findTheOldest/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ Now that you've reached the final exercise, you should be fairly comfortable get
66

77
## Hints
88
- You should return the whole person object, but the tests mostly just check to make sure the name is correct.
9-
- This can be done with a couple of chained array methods, or by using `reduce`.
9+
- There are many ways of doing this using built-in array methods like `reduce`, or even chaining multiple!
1010
- One of the tests checks for people with no death-date.. use JavaScript's Date function to get their age as of today.
Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
const findTheOldest = function (array) {
2-
return array.reduce((oldest, currentPerson) => {
1+
const getAge = function (birth, death) {
2+
if (!death) {
3+
death = new Date().getFullYear();
4+
}
5+
return death - birth;
6+
};
7+
8+
const findTheOldest = function (people) {
9+
return people.reduce((oldest, currentPerson) => {
310
const oldestAge = getAge(oldest.yearOfBirth, oldest.yearOfDeath);
411
const currentAge = getAge(
512
currentPerson.yearOfBirth,
@@ -9,11 +16,25 @@ const findTheOldest = function (array) {
916
});
1017
};
1118

12-
const getAge = function (birth, death) {
13-
if (!death) {
14-
death = new Date().getFullYear();
15-
}
16-
return death - birth;
19+
/* ALTERNATIVE SOLUTION
20+
const getAge = function (person) {
21+
// The nullish coalescing assignment operator
22+
// only does the assignment if the left side is "nullish" (evaluates to undefined or null)
23+
// if the left side has any other value, no assignment happens
24+
// here, we use ??= to set the current year for our subtraction below only if there is no year of death
25+
person.yearOfDeath ??= new Date().getFullYear();
26+
27+
return person.yearOfDeath - person.yearOfBirth;
28+
};
29+
30+
const findTheOldest = function (people) {
31+
const peopleOldestToYoungest = people.toSorted(
32+
(person, nextPerson) => getAge(nextPerson) - getAge(person),
33+
);
34+
35+
const oldestPerson = peopleOldestToYoungest[0];
36+
return oldestPerson;
1737
};
38+
*/
1839

1940
module.exports = findTheOldest;

0 commit comments

Comments
 (0)
0