Recreating the Lodash Library dropWhile


#1

I have been struggling to complete the dropWhile method.

  1. Add a method to our _ object called dropWhile .
  2. Add two parameters to this method: array and predicate .
  3. Within the method, create a new variable called dropNumber and set its value equal to the return value of a call to findIndex on array .
  4. Pass an anonymous callback function to findIndex that takes two arguments: element and index .
  5. Within the callback function, return the negated return value of predicate called with element , index , and array . We negate the value (use ! ) since we are looking to drop elements until the predicate returns a falsy value. However, .findIndex() is looking for the first truthy value. So, we make every truthy value falsy and vice versa to get the value we are looking for.
  6. After the entire dropNumber declaration, create a new variable called droppedArray and set its value to the return value of this.drop() called with dropNumber . We are using this since .drop() is a method on the _ object which is the current object we are working from, and therefore the current value of this . Calling _.drop() would also have worked but is a less common practice.
  7. Return droppedArray from the method.

Once you’ve finished implementing this method, move on to the next step to test it.

My code so far:

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

I get the following response from the test module:

It is not clear to me why it it not working. Does anyone have a suggestion?


#2

Compare what it should do to what it does.
You should get yourself a good enough understanding of what you’re writing so that you can say what needs to happen. Those are then things which you can investigate whether they do happen by printing out what is being done.
A good idea is to start somewhere in the middle rather than to print out everything that is done, and then continue to narrow down where the problem can be.
A good first place to look might be whether dropNumber has the value you need - that tells you if the problem is with the dropping, or if it’s with figuring out how many should be dropped.


#3

This might be the source of the problem.


#4

Let try to explain what we are doing here:
findIndex --> return the index of the first element in the array that satisfies the provided testing function .
So this function will do our work and asigne it to dropNum.

  1. Within the callback function, RETURN the negated return value of predicate called with element , index , and array .

Let try to explain what we are doing here:
findIndex --> return the index of the first element in the array that satisfies the provided testing function .
So this function will do our work and asigne it to dropNum.

  1. Within the callback function, RETURN the negated return value of predicate called with element , index , and array .

This is where one should read it more slowly until one understand what to return…

I found the solution writing the code on paper super slowly until I get it…:sunglasses:

BUT I still don’t understand why we call the anonymous function and not directly de predicate??:confused:


#5

Because the predicate function is not what we wish to return. The droppedArray is the return, and we need to run the callback on findIndex to get an index to pass to the drop method to derive it.

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