Slice and Splice - why can't you do this?

Tell us what’s happening:

Hi all, just wondering why you cannot Splice in array into another array? When I do a console.log it seems to return a similar result. Well not exactly. In the result field in freeCodeCamp it returns something like:

4,1,2,3,5,6

However in Chrome Dev tools it returns: [4, Array3, 5, 6] which still has the same numbers in sequence. It just doesn’t display them like [4, 1, 2, 3, 5, 6] as it would if you loop through and splice them in individually. Is this not passing the tests because it is looking for them displayed as in the loop method?

Thanks!

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  var newArr = arr2.slice();
  newArr.splice(n, 0, arr1);
  
  console.log(newArr);

  return newArr;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:69.0) Gecko/20100101 Firefox/69.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice/

The difference is between a flat array and a multi-dimensional array.
[4, 1, 2, 3, 5, 6] is a flat array with 6 elements.
[4, [1, 2, 3], 5, 6] is a multi-dimensional array with 4 elements. The second element is an array.

Got it. Thanks much. So what they want to see here is a single array with all elements on the same level. Thanks for the clarification. Helps in the learning. :slight_smile:

I’m glad I could help. Happy coding!