Re-creating lodash library project, drop

[https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash]

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!

2 Likes

I was so close to having this answer!!! my one mistake was slicing from array[0] to array[n], when I swapped the [n] to the start position and dropped in array.length I passed all the tests. So close to solving it on my own. :sob:

1 Like