# Basic Algorithm Scripting: Slice and Splice, Unaccepted correct solution?

Hi, this is my solution to the challenge in the title, as you can see my code produce all the requirements given in the challenge yet FCC app won’t accept it, why?

``````function frankenSplice(arr1, arr2, n){
let tempArr2=arr2.slice(0);
tempArr2.splice(n,0,arr1);

return tempArr2;

}

console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2))
``````

Open the browser console, you will notice that you have a multidimensional array instead of the monodimensional you need

What is the difference? Please elaborate

You may refer to the explanation I posted a couple of days before.
The questioner almost did the same thing as you’re doing…
`tempArr2.splice(n,0,arr1);`        `<---` I wonder why?

2 Likes

For your answer there, it dosen’t answer my question first because I didn’t remove any thing in my code and second because I am asking why the rejection of my code albeit it gives the same output?
And for your wonderment obviously everyone will be confused if there code output is simmilar to the answer yet it still rejected.

Your result is a multidimensional array, but you don’t see the difference in FCC console, use the browser console to check the result and you will see

1 Like

Yes, use the developer’s console which is built into your browser. It’s a thing most modern browsers include, as (for the most part) developers are using the browsers for building and testing code.

But in your code, you don’t actually output the array to the console. The tests might, but as a developer, it’s a useful thing to know how. Looking at your code, note the line I’m adding:

``````function frankenSplice(arr1, arr2, n){
let tempArr2=arr2.slice(0);
tempArr2.splice(n, 0, arr1);

// Use this for debugging your code, and make sure the developer
//   tools are open.
console.log(tempArr2);

return tempArr2;
}
``````

That’s an easy way to see what’s going on in the array. However, I tend to go one step further, as the developer console in Chrome does some weirdness:

``````function frankenSplice(arr1, arr2, n){
let tempArr2=arr2.slice(0);
tempArr2.splice(n, 0, arr1);

// This takes your array, and represents it as a string, then outputs
//  that to the console. Again, use your dev tools!!
console.log( JSON.stringify( tempArr2 ) );

return tempArr2;
}
``````

The reason I use this approach is that I find it easier to view the array as a string, and because once I have that string, the browser won’t be changing the string value on me.

But here’s what you’ll get when you run that second one, popping up in the console:

``````[4,[1,2,3],5,6]                                                  VM178:9
[4,[1,2,3],5]                                                    VM178:9
["a",[1,2],"b"]                                                  VM178:9
[[1,2,3,4]]                                                      VM178:9
``````

That bit on the right side isn’t really relevant for your use, but that left side definitely is. That is the actual value of tempArr before you return it. Each pair of `[...]` indicates an array. What you’re being told is that you are returning

``````[4,[1,2,3],5,6]
``````

when the test is looking for

``````[4,1,2,3,5,6]
``````

Can you see the difference?

2 Likes

Hi everyone, both ( ieahleen, snowmonkey) your responses helped me understand the issue, I appreciate it, and I’ve even been able to solve it with a “one liner”:

``````let tempStr = arr2.slice(0, n).concat(arr1).concat(arr2.slice(n));
``````

However I will choose to accept snowmonkey answer because it is more elaborate It will serve as a reference for beginners that will face the same issue, though I suggest to edit the post a little:

What do you mean by that? when you have put the console.log() before the "return" the output is redundant, the second code you provided, however, is different and clean.

Regards

True, it isn’t strictly necessary to put the`console.log()` just before the return statement – but you only wrapped the given test case in a console.log(). That doesn’t take into account the fact that there are often quite a few other tests that are being run, and you won’t get the result of those others logged to your console. So, for example, when you get to the pig latin challenge, being able to see all the strings’ output from within your function will be VERY handy.

1 Like

I was challenging the problem while doing the same worries, but I read your explanation and settled my troubles.
Thank you.

Hello,
I have a question here:
what is the problem if I use these statement to assign`arr2` to `tempArr2` like this:

``````tempArr2 = arr2;
``````

How `arr2` has been modified.
someone has any idea HOW???

you are not copying the array with that, you are adding an other way to reference the array - if you look at your code using this tool you will see the visual representation of what’s happening: http://pythontutor.com/javascript.html#mode=edit
to copy an array you need to use concat, slice o spread operator

2 Likes

thank you, I got that.

1 Like