TheOdinProject/javascript-exercises

Exercise 12_findTheOldest: Alternative solution

Closed this issue · 4 comments

Complete the following REQUIRED checkboxes:

  • I have thoroughly read and understand The Odin Project Contributing Guide
  • The title of this issue follows the location for request: brief description of request format, e.g. Exercises: Add exercise on XYZ

The following checkbox is OPTIONAL:

  • I would like to be assigned this issue to work on it

1. Description of the Feature Request:

When tackling this exercise I came up with in my view a clear and consise solution. Instead of using a helper function and the .reduce mathod, I used the .sort method and return the first item in the sorted array.

2. Acceptance Criteria:

  • A peer review of the alternative solution
  • Inclusion of the solution in the exercise

3. Additional Information:

n.a.

Sorry I'm not sure I understand what you're looking for in this issue, it's hard to say if we would want it added as an alternative solution without the solution being provided for us to review.

Just a heads up that the provided solution file for exercise 12 already contains an alternative solution that makes use of sorting (so 2 solutions are already provided)

I am sorry for the inconvenience.
My solution is the following:

const findTheOldest = function(people) {
    const sortedPeople = people.sort((a, b) => {
        if ((a.yearOfDeath || 2024) - a.yearOfBirth > (b.yearOfDeath || 2024) - b.yearOfBirth) {
            return -1
        } else {
            return 1
        }
    })
    return sortedPeople[0]
};

I did see the alternative solution, but it still uses an extra helper function whereas my solution does the age calculation in the conditional statement. If it has no added value to add this, that is fine as well. But I figured it could make sense, given the tasks we came across in the lessons prior.

Thanks @EKlijnsma.

I personally don't feel we need to include every possible alternative solution, else everyone will propose their own variant (of which there are many approaches) and we'd just get a clogged solution file.

I think what we have now is sufficient, as it demonstrates two very different approaches. Any more and I feel we just have diminishing returns, adding alternatives for the sake of it. Therefore, I'll close this issue without further action. Still, thank you for the suggestion and good job completing the exercise!