How do parameters in this anonymous function get their value?

dropWhile(array, predicate) {
    let dropNumber = array.findIndex((element, index) => {
      return !predicate(element, index, array)
    });
    let droppedArray = this.drop(array, dropNumber);

    return droppedArray;
  }

This is the solution code to one of the functions of this exercise. I understand the logic of how the value produced by the anonymous function is used to then find the index of the value we’re looking for; however, I don’t understand how the anonymous function is getting the values for the parameters element and index. Where are the correct values to these parameters coming?

1 Like

Hi,

With methods, it’s always good to look up documentation. Array.prototype.findIndex() - JavaScript | MDN

The findIndex() method returns the index of the first element in the array that satisfies the provided testing function . Otherwise, it returns -1 , indicating that no element passed the test.

That is to say, the method will iterate automatically through an array looking to fulfill a condition. The condition in the code you provided is that the predicate invocation returns false. (In this case, we assume that predicate is a function that takes in these 3 arguments and returns a boolean value).

JS functions inspired by functional programming usually have the pattern of (element, index, array) as options for parameters (others fp functions include: map(), reduce(), filter()).

Notice under syntax in the documentation you have the potential ways to handle parameters in calling it:

//from MDN docs
// Arrow function
findIndex((element) => { ... } )
findIndex((element, index) => { ... } )
findIndex((element, index, array) => { ... } )

Thank you so much for such a thorough and detailed explanation! That all makes sense!

1 Like