Basic Algorithm Scripting: Slice and Splice (HELP with aspect of directions)

I’m trying to understand the directions for this algorithm and one of the directions is confusing me:

“Begin inserting elements at index n of the second array.”
The splice() and slice() I am looking up and working through these, but can someone explain this a bit differently. I’m unsure if I should use splice() with the index n with arr1 as one of the parameters. Or, if n needs to be used as a parameter when I splice from arr1 or if all of the above is wrong and it means something else.

Help please.

Thank you

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

The examples in the MDN docs should help, just in case you haven’t already read these docs.

The challenge wants you to take the first array and put the elements from it somewhere inside the second array.

Splice will allow you to do this and slice will help you to make a copy of the array(s) since it doesn’t want you to mutate the arguments.


There are a few different ways you could do this. If you aren’t already use an environment you can experiment with like https://repl.it/ or chrome snippets or a local html + js files combo etc.

1 Like

Walking through first test case

frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5]

In the second array [4, 5]
[4, n 5] at the point of index n=1
insert all elements of first array [1, 2, 3]
to get [4, 1, 2, 3, 5]

You could do these insertions one at a time or all at once depending on your Javascript fluency.

The input arrays should remain the same after the function runs.

This requirement adds a bit of complexity but is a good practice

I’m a bit perplexed as to why what I’m doing wrong.

I created new variables so that could slice and splice from arr1 & arr2 without altering the original arrays. The issue is probably in how I’m trying to insert a variable into the method parameters of another variable…yet, I don’t understand why.

Help please.


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

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

Look at the return value from the splice method. Splice does not return what you think it does.

So, it’s not working and I need to do some more thinking but I’m not sure why I’m failing the test concerning keeping the original array of arr2.

I have arranged so that array has been kept within keepArr, so that it is preserved. Where am I going wrong with this?

Otherwise, I attempted to save my splice() within the keepArr2 container, but it registers nothing when I console.log it. Yet, when I console.log arr2 it returns something that’s almost a right answer [4,5,1,2,6]

I guess I’m thinking that what I’ve written should really return something entirely different than what is actually returning. I think this is something to do with how I’m using my variable containers but I’m not sure exactly where that fault lies.


function frankenSplice(arr1, arr2, n) {
  // Make a copy of the second array
  const keepArr = arr2.slice(0,3);
  //Make loop to run through first array 
  for(let i = 0;i < arr1.length; i++){
    //splice each items into the array as an argument
    if(i>0){
    let keepArr2 = arr2.splice(-1,0,i);
    }
  }
  console.log(keepArr2);
  return keepArr2;
}

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

Nevermind! I figured it out! I should’ve been using the spread operator. Thanks for your help.

This is my code but still wondering why it didnt work???
unction frankenSplice(arr1, arr2, n) {
// It’s alive. It’s alive!
var sliced = arr2.slice();
var result = sliced.splice(n, 0, …arr1);

return result;
}

frankenSplice([1, 2, 3], [4, 5, 6], 1);
I created a new variable to make a copy of arr2 using slice. So that array is not changed, but what is the missing thing over here???

try adding console.log("result is " + result) before your return statement

the splice method returns the removed elements (in this case an empty array), and instead change the array on which it is applied

I know, but I can slice both arr1 and arr2 into new variables, so arrays stay in original way, and then apply splice on those variables if you understand me, but still not working…

the last 2 test passed, if so, meaning original arrays are not changed, why the rest of test not passed, as I typed the right way with splice???

try using console.log() to check the value of your variables
I have added them to your code now, copy and paste this and see what the values are, result is not what you think.

function frankenSplice(arr1, arr2, n) {
   // It’s alive. It’s alive!
   var sliced = arr2.slice();
   console.log("before splicing 'sliced' is [" + sliced + "]");
   var result = sliced.splice(n, 0, ...arr1);
   console.log("'result' is [" + result + "]");
   console.log("after splicing 'sliced' is [" + sliced + "]");

   return result;
}

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

Here is my code

function frankenSplice(arr1, arr2, n) {
let newArr = arr2;
newArr.splice(n, 0, …arr1);
return newArr;
}

I pass all the tests except the last one, It says ‘The second array should remain the same after the function runs’

what am I missing?

PS: I have added ```console.log(arr2); before the closing } of the function, that’s not the answer.

let newArr = arr2;

To see the result of the console.log add it before the return statement

This is not copying the array. To copy the array you can use slice(), concat() or the spread operator ...

1 Like

I made two changes…

  1. let newArr = arr2.slice();
  1. added console.log(newArr) before the return statement…

It worked! Thanks…

Console.log produces the desired result, but I am not passing the tests.

function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!
  let newArr = [];
  newArr = arr2.slice(0,n);
  newArr.push(arr1);
  newArr.push(arr2.slice(n,));
  console.log(newArr);
  return newArr;
}

frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);

console.log produces head,shoulders,claw,tentacle,knees,toes

Try with the browser console, you have a multidimensional array
Your result is actually like this:

[ 'head',
  'shoulders',
  [ 'claw', 'tentacle' ],
  [ 'knees', 'toes' ] ]
1 Like

Thank you for the explanation. Is there a statement that displays multidimensional arrays correctly?

There are many ways to see the arrays as they are… examples:

1 Like

Hello fellows!

I have just done this one and I would like to share my solution.

Pretty short. What do you think?

 function frankenSplice(arr1, arr2, n) {
    arr2.splice(n, 0, arr1); 
    return arr2.flat()
  }