Fundamentals Part 5


#1

Hello Everyone,

I am hoping for some clarity here. I am currently working through Fundamentals 5 and I am pretty lost. I find myself having to refer to the solutions and honestly I am becoming more confused. I am a little discouraged because I feel as if I would have in no way found the solution on my own with my current knowledge and I am hoping that is a common experience.

For example in the solution for “findTheOldest” there are several things I am struggling with.

    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

I apologize if I am not utilizing any features of this forum, this is my first post and I am frankly just winging it.

Firstly, on what would be line 4 what is currentPerson? It is not referenced anywhere, so how does it have a value? Unless there is some functionality within the ternary statement that I am not aware of?

Again I am confused in the same way with “death” and “birth”. Death is referenced within the anonymous function getAge, but only if it doesn’t exist within the object? Same for “birth”. I only see “birth” referenced once but it does not have a value?

Any help with this is greatly appreciated!


#2

This is based on my knowledge of reduce.

Following happens in anonymous function :

  1. Oldest will be initially the first element in the array.
    ( there is one more way of using reduce, please explore that )
  2. currentPerson will be second element.
  3. After the calculation oldest will be set to value returned by the anonymous function.
  4. currentPerson will change to third, you do the calculation and return the new value of oldest. Then currentPerson will change to fourth and so on.
  5. Finally when you reach the end of the array last returned value of the anonymous function i.e oldest will be returned as the result of the reduce.

try this inside anonymous function passed to reduce, you will understand better
console.log("Oldest : “,oldest,” Current person : ",currentPerson);

Example:

[1,2,3].reduce( ( previousResult , nextItemInTheArray ) => {`
return previousResult += nextItemInTheArray;
} , <an optional initial value to start with> );
// if you dont give an optional initial value, the previousResult will be the first element in the array
// preciousResult is the value returned by you in previous iteration of reduce.

About getAge… birth and death are names given to the parameters, they do not exist in that context. Birth and death have to be passed to getAge to be able to use them.

In the line const getAge = … you are storing the function in getAge.
Similarly in the line const findTheOldest = … you are storing the function that returns the result of array.reduce

Read the above lines slowly if you don’t understand in one go

Check javascript.info to understand more about Array functions, and functions better.


#3

This makes complete sense now… Thank you! I don’t know why that didn’t hit me before haha…