Recreating the Lodash Library dropWhile

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:
foutmelding

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

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.

This might be the source of the problem.

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:

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;
  },
1 Like
dropWhile(array, predicate) {
        //符合的都删除
        let dropNumber = {};
        let dropArray = {};
        for (var index = 0; index < array.length; index++) {
            let element = array[index];
            if (!predicate(element, index)) {
                dropNumber = index;
            }
            dropArray = this.drop(array, dropNumber);
        }
        return dropArray;

This is what I came up with.

    dropWhile : function(array, fn){
      let idx = 0;
      while (fn(array[idx], idx)){
             idx+=1;
             }      
      return this.drop(array, idx);
    }
1 Like

You’re just missing a return keyword before the line:

!(predicate(element, index, array))

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

Hi Guys,
I’m having a similar problem.
Except its a different error here

Can any one point out the error

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

The default expression cannot be a statement. number is either n or it is 1.

May not be the problem, but one all the same.

1 Like

Finally figured it out and worked it.
Thank you

1 Like

This is what I ended up with. Is it ok I did it differently?

dropWhile(array, predicate) {
for (element in array) {
let current = predicate(array[element]);
if (current === false) {
let start = array[element];
return array.slice(start + 1);
}
}
}

This is what I ended up with. Is it ok I did it differently?

dropWhile(array, predicate) {
  for (element in array) {
   let current = predicate(array[element]);
   if (current === false) {
   let start = array[element];
   return array.slice(start + 1);
     }
  }
}

What was the result of node test/drop-while.js?

That is not valid JS. Use the of keyword, instead.

Everything passed for some reason. I also worked it out the way I was instructed to as well. I just looked up for…in VS for…of and I think I understand the difference. Basically for…in loop is used to iterate over properties of an object and for…of loop will iterate over any data type that is iterable. Is that more or less correct?

1 Like

Yes. An array is special type of object, but an object all the same. The in operator is for iterating over object members, though not exactly suited to iterating over array elements. There is the off chance that stray data could leak in. This is why the of keyword and construct came into being in ES2015/ES6. We now have the freedom to address the actual values of elements without risk of contamination.

Arrays, strings and collections are all indexed. These are the iterables for which in is not a viable keyword/operator. of paves the way.


Aside

Be sure to come back to this project repeatedly. The abstracts are plentiful when we once discover them.

Thank you for the well said explanation and for your time, I’m learning slowly but surely. I will heed your advice.

1 Like

I had a similar problem. Thanks for answers.