Splice and slice help needed

I’m trying to solve this problem without using loops
why isn’t this solution correct?

my code so far


function frankenSplice(arr1, arr2, n) {
let myArr = [];
myArr.push(...arr2) 
myArr.splice(n,1,...arr1, arr2[n])
return myArr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);

Challenge: Slice and Splice

Link to the challenge:

Create a copy of arr2 using the slice method. Now splice arr1 into the arr2 clone (use the spread operator).

1 Like

if I add these two console.logs I can see what the tests uses:

function frankenSplice(arr1, arr2, n) {
  console.log({arr1, arr2, n})
let myArr = [];
myArr.push(...arr2) 
myArr.splice(n,1,...arr1, arr2[n])
console.log(myArr);
return myArr;
}
frankenSplice([1, 2, 3], [4, 5, 6], 1);

pressing Run tests, in the console appear this:

// running tests
All elements from the first array should be added to the second array in their original order.
// tests completed
// console output
{ arr1: [ 1, 2, 3 ], arr2: [ 4, 5 ], n: 1 }
[ 4, 1, 2, 3, 5 ]
{ arr1: [ 1, 2, 3 ], arr2: [ 4, 5, 6 ], n: 1 }
[ 4, 1, 2, 3, 5, 6 ]
{ arr1: [ 1, 2 ], arr2: [ 'a', 'b' ], n: 1 }
[ 'a', 1, 2, 'b' ]
{ arr1: [ 'claw', 'tentacle' ],
  arr2: [ 'head', 'shoulders', 'knees', 'toes' ],
  n: 2 }
[ 'head', 'shoulders', 'claw', 'tentacle', 'knees', 'toes' ]
{ arr1: [ 1, 2, 3, 4 ], arr2: [], n: 0 }
[ 1, 2, 3, 4, undefined ]
{ arr1: [ 1, 2 ], arr2: [ 'a', 'b' ], n: undefined }
[ 1, 2, undefined, 'b' ]
{ arr1: [ 1, 2 ], arr2: [ 'a', 'b' ], n: undefined }
[ 1, 2, undefined, 'b' ]
{ arr1: [ 1, 2, 3 ], arr2: [ 4, 5, 6 ], n: 1 }
[ 4, 1, 2, 3, 5, 6 ]

look at this:

{ arr1: [ 1, 2, 3, 4 ], arr2: [], n: 0 }
[ 1, 2, 3, 4, undefined ]

you are putting undefined in your array. arr2[0] is undefined
try an other way of adding elements to the array

1 Like