Slice and Splice wont take my solution

Tell us what’s happening:
This solution works as far as I can tell but wont actually accept it. Is there something I am missing?

Your code so far


function frankenSplice(arr1, arr2, n) {
  var arr3 = [];
  arr3 = arr2.slice();
  arr3.splice(n,0,arr1);
  console.log(arr3);
  return arr3;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

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

You are creating a multi-dimensional array rather than a flat array.

2 Likes

@ArielLeslie is right your splicing in “[1, 2, 3]”. When you need to splice in 1, then splice in 2, then splice in 3. When you console.log(frankenSplice([1, 2, 3], [4, 5], 1)); it will look correct on the test console but it is not correct. There is a hint available for this challenge.

1 Like

You can totally add all the elements at once, splice supports many arguments, and it is easier, you just have to remember the right operator

1 Like

Thank you all I understand now why it wouldn’t work.

function frankenSplice(arr1, arr2, n) {

  let newArray=[];

  let arr11=arr1;

  let arr22=arr2;

  let arr2Spliced=arr22.splice(n);

 

  //arr2=4

  let arr2Sliced= arr22.slice(n);

  newArray=arr22.concat(arr11,arr2Spliced);

  console.log(arr2);

  return newArray; 

}

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

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.