Slice and Splice(can't pass tests with correct output)

Tell us what’s happening:

Hi everyone, I’m just wondering if anyone knows why this code doesn,t pass the test even though I get the correct output every time. I already fixed it by replacing this arr1.slice(i, i+1) with this arr1[i] inside the for loop but I just don’t understand why that fixes the issue if still I get the same output.

Thanks in advance for your help.

Your code so far


function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let arr3 = [...arr2];
  for(let i = 0; i<arr1.length;i++){
    arr3.splice(n++, 0, arr1.slice(i, i+1));
  }
  return arr3;
}

console.log(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/77.0.3865.90 Safari/537.36.

Link to the challenge:

You are creating a multi-dimensional array instead of a flat array.
See it in action here:

1 Like

Oh that’s right, Splice return an array and not the element itself. Thanks a lot for the clarification. You’re the boss.

I’m glad I could help. Happy coding!

Same issuw with me , The challenge not passing
my code

function frankenSplice(arr1, arr2, n) {
  //It's alive. It's alive!
  let arr2Temp = arr2.slice()
  let arrTemp = arr2Temp.splice(n,arr1.length,arr1.slice());
  console.log(arrTemp)
  console.log(arr2Temp.concat(arrTemp))
  return arr2Temp.concat(arrTemp);
}

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

Please help

You are also creating a multi-dimensional array.

1 Like

ohh next time i will run and check my code in local editor( VS Code). The FCC browser console display format is the issue . I was chopping and changing my code but result was showing similar and was not able to debug properly and i was stuck here for last 1 hr.
Thanks.

1 Like

The results window in fCC passes the result through the toString() method. In many cases, that is necessary but when you use toString() on an array, you don’t see the braces.

See below:

You can use your browser’s console or another editor to see results without the toString().

2 Likes