Slice and Splice - what am I missing here?

Tell us what’s happening:

What am I missing here? I seem to be returning what the problem is asking me to return. Is a loop really the only option here?

Your code so far


 function frankenSplice(arr1, arr2, n) {
 let newArray1 = arr1.slice();
 //console.log(newArray1);

 let newArray2 = arr2.slice();
 //console.log(newArray2);

 newArray2.splice(n, 0, newArray1);  

 return newArray2;
}

console.log(frankenSplice([1,2,3], [4,5], 1));
console.log(frankenSplice([1,2], ["a", "b"], 1));
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2));

Your browser information:

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

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

I suggest of using console.log(JSON.stringify(...)), or browser console or a different editor to see what the issue is

1 Like

Your answer is correct except for one thing. Your final answer comes out as a two-dimensional array. The challenge wants you to return a one-dimensional array.

For example:

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

it should instead return
[4, 1, 2, 3, 5, 3]

Look up how you can flatten your final answer. Actually, there is a super simple thing you can add to your current code that would fix it.

1 Like

this is SUPER helpful – I’ve never used Stringify before. thank you!

This rules! I love learning new JS methods – I found the method you were referring to and it fixed it. Thank you!