Slice and Splice. Individual tests are correct. General test - no

I wrote the following code:


function frankenSplice(arr1, arr2, n) {
  let arr = arr2.slice();  
  arr.splice(0, 0, arr.splice(0, n), arr1);
  return arr;
}

When I do the tests individually, I get the right results. Here they are:


console.log(frankenSplice([1, 2], [3, 4, 5, 6], 2)); //3,4,1,2,5,6
console.log(frankenSplice([1, 2, 3], [4, 5], 1)); //4,1,2,3,5
console.log(frankenSplice([1, 2], ["a", "b"], 1)); //a,1,2,b
console.log(frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2)); //head,shoulders,claw,tentacle,knees,toes

But when I press the “Run the Tests” button the result is wrong. The tests 1, 2, 3 and 4 are incorrect. Tests 5 and 6 are correct.
What’s wrong with my code. Please help!

Here’s another version of the code that works the same way as the first one. And no errors in the final test gives the same.


function frankenSplice(arr1, arr2, n) {
  let arr = arr2.slice(); 
  let arr2Del = arr.splice(0, n);
  arr2Del.splice(n, 0, arr1, arr); 
  return arr2Del;
}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

Your code is creating a mulit-dimensional array. The expected returned value is a 1D array. The reason you can’t tell that this is happening is because console.log() converts arrays to a string.

To add to what awesome @ArielLeslie said, you can use the browser console - there you can see your array as an array instead of converted to string

As a developer it is a good habit to use the browser console

Thank! I will sort this out.