TypeError: this.drop is not a function

I’m stuck. I’ve been comparing my code to the walkthrough and to some of the code on here, but I can’t figure out why my code won’t pass the test. If anyone can see what I did wrong, could you please let me know?

_.drop = (array, n) => {
  if (!n) {
    n = 1;
  };
  let droppedArray = array.slice(n);
  return droppedArray;
}

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

And then it says, TypeError: this.drop is not a function
I would really appreciate some help on this. Thank you!

the .drop stuff seems to work fine.
I put let _ = { }; at the beginning to create the object, and tested _.drop
so the problem may be in another part.

let _ = { }; _.drop = (array, n) => { if (!n) { n = 1; }; let droppedArray = array.slice(n); return droppedArray; }; _.dropWhile = (array, predicate) => { const dropNumber = array.findIndex((element, index) => { return !predicate(element, index, array); }); const droppedArray = this.drop(array, dropNumber); return droppedArray; }; let letters = ['a', 'b', 'c', 'd']; let x = _.drop(letters, 2); console.log(x);

What exercise or lesson is this for?

Full-Stack Engineering course, Lodash project
Full-Stack Engineering, Lodash

The general idea is to build the methods inside the lodash object.

const _ = {
  drop (array, n) {

  },
  dropWhile (array, predicate) {
    // this.drop is now in the _ context
  }
}
2 Likes

See Arrow function expressions - JavaScript | MDN

You are using this inside an arrow function which doesn’t work. You could add the methods inside the original object as suggested by @mtf (and the instructions, I believe), or if you insist on adding the methods outside of the object body, use function expressions:

const _ = {};

_.dropWhile = function(array, predicate) {
  //code
}
2 Likes

Thank you! I don’t know how I missed that. :woman_facepalming:

1 Like

Thank you! I guess I wasn’t paying enough attention. I don’t usually put the methods outside the original object, but I should have thought to try function expressions when the arrow function wasn’t working. Thank you!

1 Like