Re-creating the Lodash Library - drop() method


#1

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

Hi,

I am super stuck at the moment right now.

These are the instructions:

  1. Add a method to our _ object called drop .
  2. Add two parameters to this method: array and n .
  3. Within the method, use an if statement to check if n has been set.
  4. Within the if statement block, set n equal to 1 .
  5. Outside of the if statment, create a new variable called droppedArray and set its value to be a copy of the array missing the first n elements by using .slice() .
  6. Return droppedArray from the method.

This is what I have got :

drop(array, n) {

if(n) {
  n = 1;
}  
const index = array.indexOf(n);
let droppedArray =array.splice(index, 1);



return droppedArray;

}

And my result is this :

I am almost stuck for half a day right now. Did so much research and nobody seems to struggle with this question because there is nothing to find in the forums about it.


#2

you only really need this

let droppedArray = array.slice(n, array.length);

In your implementation you are looking for n as an element in the array but that is not what you are supposed to do.
You just need to slice from the index index provide , n , to the end of the array.

_.drop = function(array, n){
  //is n provided? if not defualt  to 1
  // you can also use  --> n = n || 1;
  if(n === 'undefined')
  {
    n = 1;
  }
  //store sliced array
  let droppedArray = array.slice(n, array.length);
  return droppedArray;
};

Does that make sense?
you can improve it further with some error handlers
Cheers