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.
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
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 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
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