Lodash dropWhile - weird test error

On the Lodash project https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash, my code is throwing an failed test I cannot understand.

Although we are instructed/guided by the Codecademy instructions to evoke the .drop() method, it repeatedly throws errors, though I am not sure how to achieve the objective of this exercise without it. Help pls?

  1. 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.
  drop(array, number) {
    if (number===undefined) {
      number = 1; } let droppedArray = array.slice(dropNumber);
    return droppedArray;
// dropWhile creates "slice" of array excl. elements dropped from beginning; elements are dropped until predicate returns falsey, method takes two params: (array, predicate function), and predicate is invoked with three: (value, index, array)
  dropWhile(array, predicate) {
    // elements dropped from beginning while a loop returns truthy values -- use a while loop???? No. Use .findIndex since that's exactly what this method does, i.e., iterates through array until element is found that returns specific value 
    let dropNumber = array.findIndex = (element, index) => !predicate(element, index, array); // create var and set equal to return value of call to findIndex on array, pass anon callback function that takes two params: (element, index), within callback return negated value of predicate called with (element, index and array)
    let droppedArray = this.drop(dropNumber); // create new var, set value to return value of this.drop() or by calling _.drop() -- HAVE TRIED & TESTED BOTH & THIS TEST FAILS AS (TypeError: array.slice is not a function...): why?
    return droppedArray; // returns new array

.dropWhile() Tests:
1 - .dropWhile() is defined - Passed!
number = 1; } let droppedArray = array.slice(dropNumber);
TypeError: array.slice is not a function
at Object.drop (/home/ccuser/workspace/underscore-javascript-capstone/
at Object.dropWhile (/home/ccuser/workspace/underscore-javascript-capstone/_.js:76:26)
at Object. (/home/ccuser/workspace/underscore-javascript-capstone/test/drop-while.js:17:135)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)

If you look at your error message, it shows you that the error is occurring in the drop method. Look at where in your drop_while method you are calling drop:

Now look at your drop method:

Examining this code, what values are assigned to array and number?

I see you’ve edited your post, and now have new issues. What value are you expecting array to have here after calling the function like this let droppedArray = this.drop(dropNumber);?

This method takes two arguments. The first one should be an array, the second is the index to be used in the slice method. If you pass something other than an array that doesn’t have a slice method like a number for example, you get the error you have.

Also, by changing number to dropNumber in this line:

you’ve broken your drop method. Inside your drop method dropNumber is undefined. You had that part right before.

1 Like

Yes, I realized after I made my original post that I had declared droppedArray twice, which wouldn’t necessarily break the code but which would certainly cause the Node test to fail.

So I reworked my code so that it correlated with the instructions, specifically:

…create a new variable called droppedArray and set its value to the return value of this.drop() called with dropNumber

I understood it to mean and wrote this: let droppedArray = this.drop(dropNumber) but after looking more closely at .drop() specifically drop(array, number) it’s clear I am needing to pass in an array and a number, and like you said:

The first one should be an array, the second is the index to be used in the slice method.

But then you say this immediately following that which confused me:

So I’m no closer to a solution than I was because I thought I needed to pass an array and a number (index) into the .drop() method.

Which leads me back to my original query:

I’m stuck again.

The first argument needs to be an array. The second argument a number which will serve as the index. Since you only passed one argument, it gets assigned to the array parameter in your drop method, and raises the error. I’m about to be offline for several hours, but the gist is that you have an array, and a predicate function passed to dropWhile. You use the predicate function to determine the value of dropNumber, then pass the array, and dropNumber to drop as instructed.

You’ve also got an issue here:

I will be offline for the next several hours, so see what you can do, and possibly someone else can continue to offer assistance if you need it before I get back online.

1 Like

Holy cow. THANK YOU!

Somehow I was not “getting” that dropNumber was a number…which was obviously throwing me off. It was, for some reason, stuck in my head that it was the array I was supposed to pass in (doh!).

Thanks also for pointing out the major syntax problems I had with the dropNumber declaration. Yikes.

So with your help I finally got it to pass all tests, and I think I (kind of) understand it. Here’s what I ended up with, codewise

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

$ node test/drop-while.js
_.dropWhile() Tests:
1 - _.dropWhile() is defined - Passed!
2 - Returns an array - Passed!
3 - Drops elements until predicate function returns falsy - Passed!

1 Like