Re-creating lodash library - dropWhile()

Hello everybody, i’m doing the ‘Re-create lodash library exercise’ and i’m having problem with the dropWhile method implementation.

Here’s the code i came up with

drop(array, n){

        if (!n){ n = 1};

        const droppedArray = array.slice(n);

        return droppedArray;

    }, 

    dropWhile(array, predicate){

        const dropNumber = array.findIndex((element,index)=>{

            return !predicate(element, index, array);

        });

        const droppedArray = this.drop(dropNumber);

        return droppedArray;

    }

And here’s the error i keep getting

node test/drop-while.js
_.dropWhile() Tests:
1 - _.dropWhile() is defined - Passed!
/home/ccuser/workspace/underscore-javascript-capstone/_.js:6
        const droppedArray = this.drop(dropNumber);
                                  ^

TypeError: this.drop is not a function
    at Object.dropWhile (/home/ccuser/workspace/underscore-javascript-capstone/_.js:6:35)
    at Object.<anonymous> (/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)
    at startup (bootstrap_node.js:151:9)

Why it keep saying that .drop is not a function? .drop correctly pass every test.

Can anybody help me to solve this?

Thank you

Hi @gianmarcovarrone9307
your drop method requires an array as a first argument, but from what I understand you’re passing just a number…

2 Likes

Thank you for your quick reply, i was now able to pass all the tests with the below code :smiley:

drop(array, n){

        if (!n){ n = 1};

        const 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;

    },

But it’s still not clear to me how it is working :thinking: :thinking: :thinking:

Which part exactly? The .drop() method is clear I guess?

Yes, the .drop() is not clear :sweat_smile:

Oh really? I thought that’s the easier part. Can you specify what you don’t understand about it?

why do we need to pass array as an argument in this case?

You can apply .slice() on an array or a string. That’s why you need to pass an array (or a string) to the method .drop() as a first argument, otherwise you’ll get an error.

That’s correct, sorry i did not think to that :sweat_smile: