Basic Algorithm Scripting: Slice and Splice 1 test fails, i dunno why

Tell us what’s happening:
i have passed every test on this lesson except for this one: “All elements from the first array should be added to the second array in their original order.”
it seems to me that my code is accomplishing that. i’m wondering what i did wrong.

Your code so far


function frankenSplice(arr1, arr2, n) {
let arr3 = [];
for (let i = 0; i < arr2.length; i++) {
  if (i == n) {
    for (let j = 0; j < arr1.length; j++) {
      arr3.push(arr1[j]);
    }
  }
  arr3.push(arr2[i]);
}
return arr3;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.

Challenge: Slice and Splice

Link to the challenge:

Hello~!

Taking a look at the test script, it calls for frankenSplice([1, 2, 3, 4], [], 0) to return [1, 2, 3, 4]. Yours returns [].

1 Like

hey @alexanderstclair,

You are taking items from both arrays and adding the to a third array this is why you are getting the error because you should be adding the arr1 items to the arr2, and not use a third array,.

Also this is a splice and slice test so you should be using them instead of loops, you will have to use slice atleast so you pass the The first array should remain the same after the function runs. test.

1 Like

oh weird, ok i’m trying to start it over and follow the hints.

ok cool, i will try to follow this advice and see if i can fix it.
thanks!

To clarify, you do not want to add the items directly to arr2. The second array should remain the same after the function runs. :slight_smile:

1 Like

i don’t really understand how i can add things to arr2 while keeping it the same…

use slice() mate this is why is said you will have to use slice to pass the test,
so you can use slice to copy the original array and use that

const newArray = arr1.slice()

1 Like

@nhcarrigan @biscuitmanz thanks for the help guys, i was able to figure it out with your assistance. here’s my working code:

function frankenSplice(arr1, arr2, n) {
  let arr3 = arr2.slice(0,arr2.length);
  for (let i=0; i<arr1.length; i++) {
    arr3.splice(n+i,0,arr1[i]);
  }
  return arr3;
}

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

good job mate :slightly_smiling_face: