Slice and Splice. Splice is not working

Tell us what’s happening:
I solved problem with following code.
I think this is the correct answer. But I can not test it anywere. The splice method is not working even in browser. If I made a mistake, can you guys help me to find mistake?

result.splice(n,0,…end) <---- code is doing something like this.
start from index n, delete 0 elements from array called result and add there spreaded array end!

Your code so far


		function frankenSplice(arr1, arr2, n) {
		  // It's alive. It's alive!
      	  const end = arr1.concat(arr2.slice(n));	
		  const result = arr2;
		  return result.splice(n,0,...end);
		}

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

Your browser information:

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

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

Mh, i think you should review your code a bit^^
I’m not sure what end is supposed to be but there’s definitely too much stuff up there :stuck_out_tongue:
First of all you should consider this:

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

Slice is fine for this purpose, splice is not ( it changes the array it works on).
So, as first step you can use slice to copy one of the array into a new variable. Once done you can use splice to put the other array into the new variable, using the n argument to argue where to start.

Here a couple of reference:
MDN - Splice()
MDN - Slice

Here I did almost the same thing.

  1. First line inside function takes elements of arr2 after nth index and concatinate it to the arr1. In this case array end is equal to [1,2,3,5,6];
  2. I assigned arr2 to result array in order not to change arr2. I just stored its values to the result array

Ah almost!

const result = arr2;

Here you want assigning to result the value of arr2. Unfortunately using = will not ‘copy’ the values of the array, it just assign to result the same 'address` of arr2, hence changing one will change the other. That said the logic is correct: it is a good place to use .slice(), since it makes a shallow copy of your array^^

return result.splice(n,0,…end);

Here you’re almost done: you can just substitute end with something else and it will works!

EDIT:
Get rid of the line of code where you define end, you don’t need it :wink:

2 Likes

in your code(the const end) is just concatenating the two element 5 and 6 from arr2 to arr1 ,and concat method is not suitable here
see carefully what they need in the output result ,they want the result to be like this :
frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5] , in other words they want from you to use slice and splice ,slice for copying arr2 because they want arr2 to be unchanged , and then you can use splice to add elements from copying array that you got from slice method and as it known the splice method has three parameters ,array.splice(index, howmany, item1, …, itemX) here index=n and n is from where you can start adding ,howmany should stay 0,and item1=the element you wanna add to the copied array .
This text will be blurred

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

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

Hi!

That’s a very useful explanation and example!

I really liked your approach - I’m just wondering what the need is to increment n? I’ve managed to digest the rest!

n was incremented because if it wasn’t, it would keep adding a value after the same index (n) number instead of being added dynamically as needed.