Re-creating lodash library project, drop


In the Lodash project, we are asked to write a method to recreate the drop method, which takes two arguments (an array and a number representing the number of items to drop from the beginning of the array) and returns a new array which contains the elements from the original array, excluding the specified number of elements from the beginning of the array. If the number of elements to drop is unspecified, your method should drop one element.

I don’t understand why my code does not work.
Here is my proposal:

drop (array, number) {
return (number ? array.slice(number, array.length) : array.shift());

The problem is your use of the .shift() method. .shift() is going to return the element that was removed from the array not the remaining elements. Your method would work if you use array.slice(1, array.length) in place of your .shift() method.
Happy coding!

1 Like