Basic Algorithm Scripting: Slice and Splice partially completed

So i have been stuck on this one for a bit. I tried the rubber ducky method of just writing out basic terms of calling the arrays “boxes” and trying to copy the contents of one box into another. However, I only have part of this challenge covered. I am not understanding how to get a full combined array. I’ve tried concat(), and splicing but neither work. Where am I going wrong?

Your code so far



function frankenSplice(arr1, arr2, n) {
 arr2 = arr1.slice();
 console.log(arr2); 
 arr2.splice(arr1, n);  

return arr2;
}

console.log(frankenSplice([1, 2, 3], [4, 5], 1));
// frankenSplice([1, 2, 3], [4, 5], 1) should return [4, 1, 2, 3, 5].

Your browser information:

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

Challenge: Slice and Splice

Link to the challenge:

function spliceSlice(arr1, arr2, n){
	var ar1=arr1.slice();
	ar1.splice(n,0,arr2);
	alert(ar1);
	alert(arr1);
	alert(arr2);
}
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var moreFruit = ["Lemon", "Kiwi"];
spliceSlice(fruits,moreFruit,3);

Thanks. That example makes sense, but it’s inserting two fruits in the middle. The challenge above is confusing for me as it takes the 4 and puts it in the beginning then counts through array 1, and then ends with arrays 2 last element. I am lost on how that is achieved.

<script>
function spliceSlice(arr1, arr2, n){
	var ar1=arr1.slice();
	ar1.splice(n,0,arr2);
	alert(ar1);
	alert(arr1);
	alert(arr2);
}
var fruits = [1,2,3];
var moreFruit = [4,5];
spliceSlice(moreFruit,fruits,1);
</script>
1 Like

Ahhh. I see now. I was slicing the incorrect Array. I had to slice the second array and assign it to a variable. Then splice it ! Thank you very much!!

function frankenSplice(arr1, arr2, n) {
   let newArr = arr2.slice();
   console.log(newArr); 
       newArr.splice(n, 0, arr1);
       console.log(newArr); 
       console.log(arr1);
       console.log(arr2);

  return newArr;
}

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

I am quite sure
no working solution
was posted in this thread,
in the future I will make it
clear in my post that any
code I post is for educational
value only and it will not
satisfy the tests in the
challenge.
Thank you for your guidance,
it is nice to know that you
are involved and aware of
the activity in this forum,
I look forward to more input
from you.
Thanks again.