Slice and Splice problem

Tell us what’s happening:
Please help me to correct this code as it give correct,but not on FCC

Your code so far


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

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

Your browser information:

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

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

You have two separate issues going on with your current code.

  1. splice returns an array with any elements which were removed. In your case, you specified 0 for the 2nd argument which means remove no elements, so an empty array is returned. To resolve, just implement the splice on one line and then return newArr on a separate line.

  2. When you put arr1 as the 3rd argument of the splice function it actually puts the entire array as one single element and not the individual elements of the array arr1. After the splice is performed, newArr becomes [ 4, [ 1, 2, 3 ], 5, 6 ] instead of [4, 1, 2, 3, 5, 6] which is the correct return value.

You need to “spread” arr1 out. Review the previous ES6 challenges for how to use the spread syntax to do that.

1 Like

The only way I could get it to pass was to use this:

  let arrCopy = arr2.slice(0, arr2.length);
  arrCopy.splice(n, 0, ...arr1);
  
  return arrCopy;
function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  var newArr=[];
  newArr=arr2.slice(0);
 newArr.splice(n,0,...arr1);
 return newArr;
}

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

this also works…

1 Like

thank u @camperextraordinaire
code works after applying first suggestion

why cannot be splice assigned to variable?

var arr2copy = arr2.slice(0, arr2.length);

var out = arr2copy.splice(n,0,...arr1);

    return out;

@lubodrinka
you can assign the return value of the splice into variable.But here in your code the 2nd arugment specify no element is to be remove from “arr2” and splice return empty array/no value.
hope you understand

No.
it’s petervann solution and its working.
first create the shallow copy,
but there is still arr2copy to be assigned to out variable,
who create that logic must be such an idiot.

that is different explanation. he wrote so, you can explain in two ways.

I was trying to solve the problem almost exactly the same way you did,
but I was doing […arr1] instead of simply …arr1.
I though the spread operator must be in brackets.
Anyway thanks for the answer.
By the way here’s how I finally solved it.
I found a way to flatten the array

spoiler
function frankenSplice(arr1, arr2, n) {
      // It's alive. It's alive!
      let arr3 = [...arr2];
      let arr4 = [];
    
      arr3.splice(n, 0, arr1.slice(0))
      arr4 = [].concat.apply([], arr3)
      console.log(arr3);

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