# Basic Algorithm Scripting - Slice and Splice

Tell us what’s happening:
I don’t know why the iterator is adding numbers in this format while performing splice operation…
How to resolve this??

I’m getting the output in this format!!
[ 4, 3, 2, 1, 5, 6 ]

``````function frankenSplice(arr1, arr2, n) {
let array = arr2.slice();
for (let i=1; i <=arr1.length;) {
array.splice(n,0,i);
i++;
}
return array;
}

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

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

Challenge: Basic Algorithm Scripting - Slice and Splice

your splice will insert at index n the value of i

I don’t think this is what you want?

What should I do, inorder to pass it?

I can’t give you a solution but I can answer a question if you have one about how slice or splice works for eg. or something else.

Right now your code is adding the index variable i to the array.
Was that what you were trying to do?
If not, then you will need to change the code to do something else.

If you don’t understand the objective of the challenge, I can explain that to you also, but I can’t assume what kind of help you need without you providing input to me first on that.

Did I done this code wrongly?

I think I solved it in the proper format only!
I want you to help me find out, where I’ve done the mistake?

``````const myFish = ["angel", "clown", "mandarin", "sturgeon"];
const removed = myFish.splice(2, 0, "drum", "guitar");

// myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
``````

if this is returning the items in right hand direction!!

``````function frankenSplice(arr1, arr2, n) {
let array = arr2.slice();
for (let i=1; i <=arr1.length;) {
array.splice(n,0,i);
i++;
}
return array;
console.log(frankenSplice([1, 2, 3], [4, 5, 6], 1));
``````

o/p - [ 4, 3, 2, 1, 5, 6 ]
Expected o/p - [4, 1, 2, 3, 5, 6]

Why not this is performing in that manner??

The computer is doing exactly what you have told it to do.

Here is your code that uses splice:

You wrote a for loop, and the for loop starts at index i=1
Then you call splice and ask it to add the value i (1) to the array at index n
Then you increment i to 2

Because the variable array is [4,5,6] at the start of the for loop execution, this first loop will change array to

[4,1,5,6]

Then when the loop runs again with i = 2 now, the splice will do exactly what you said it should do and try to add 2 to the array at index n

Which means the array will now be
[4,2,1,5,6]

The you increment i to 3 and the same thing happens again.

You can see how this happens easily by adding some console.log statements in your loop.

I think if you added `console.log(array)` inside the loop after the `splice` you would see what is happening.

The only way to add a new element at the same index position is to move the other elements out of the way. If you supply all the elements as a list of arguments to `splice` it can use the list to insert all of them in the order the list is in.

``````const startEnd = ['You', 'pass!'];
const middle = ['shall', 'not'];
let startingNumber = 0;

while (startingNumber < 2) {
startEnd.splice(1, 0, middle[startingNumber]);
startingNumber++;
}
// Gandalf is now Yoda
console.log(startEnd); // [ 'You', 'not', 'shall', 'pass!' ]
``````
``````const startEnd = ['You', 'pass!'];
const middle = ['shall', 'not'];
// argument list of elements, is "'shall', 'not'""
startEnd.splice(1, 0, 'shall', 'not');
console.log(startEnd); // [ 'You', 'shall', 'not', 'pass!' ]
``````
1 Like

Thanks a lot!!
You explained it very clearly…

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.