Question on Instructions - What Makes A New Array?

I am working on recreating the Lodash Library, or implementing it, in JAVASCRIPT SYNTAX, PART II.

I was going thru the drop method which I successfully implemented like this:

drop (arr, num){
    if (num){
      arr.splice(0, num);
    } else {
      arr.splice(0,1)
    }
    return arr;
  }
}

In the instructions we are told the output must create a new array…my question is:

Is a new array an array that has been renamed or an array that has been changed or both? My code passed, but is it wrong in the sense that I did not use a var to hold a new array?

And actually what the instructions say is:

  • .drop() returns a new array which contains the elements from the original array, excluding the specified number of elements from the beginning of the array

Thank you in advance for your assistance.

It might be because you declared a new variable as a parameter, and the .splice() function is modifying that local version instead of the original passed as input. Don’t quote me on that though, I don’t really understand it myself. Just an idea :slight_smile:

1 Like

I had not thought of that. lol But yeah, maybe that is it.

I’m glad if that helped :smiley:

1 Like

It did. Unfortunately I can’t do it this way tho because the next question uses this method in it’s operation…sigh lol I was proud I came up with my own answer and now I can’t use it lol.

So beware! Use the method implementation for this part of the project. :wink:

oh well, it was great while it lasted lol :slightly_smiling_face:

1 Like

Neither. A new array would be a completely independent object with no ties to the input array. For instance, newArray = arr will not be an independent object but a reference to the same object.

newArray = arr.slice()

would be new array since it has its own reference and is completely independent of arr.

Do the instructions ask us to remove one element from the input array? Or are we to truncate the array at some index, num?

Both, if there is no num, then remove one item only. If there is a num then remove specified amount.

  • .drop() takes two arguments: an array and a number representing the number of items to drop from the beginning of the array
  • .drop() 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