Hello ) need help with array function

The function works well but I can not pass the quiz. What is wrong with code

link : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

function frankenSplice(arr1, arr2, n) {
  // It's alive. It's alive!

  arr2.splice(n, 0, arr1);
 console.log(arr2);

}

frankenSplice(["claw", "tentacle"], ["head", "shoulders", "knees", "toes"], 2);

can you please write the link to this coding challenge?

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/slice-and-splice

The function does not work: it doesn’t return anything. console.log just prints values to the console to help developers debug their code.

If you fix the above, the function still will not work because you’ve missed two requirements:

…copy each element of the first array into the second array, in order.

It isn’t saying put arr1 into arr2, it’s saying put the individual values of arr1 into arr2. You are just putting one value, arr1, into arr2, not what arr1 contains.

The input arrays should remain the same after the function runs

splice mutates, so you are modifying the arrays directly, & it is a requirement that you don’t do that.

And what to do ? Start using for loop for copy elements from array ?

first you need to create a local copy of an array, so you don’t make changes to the original one, which you can do using Array.prototype.concat(), Array.prototype.slice() or the rest operator. one of these can also simply solve how to put the elements of one array in the other array

1 Like

@Quintis check out this solution.

<redacted>

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

If you want to compare your solution to others, use the Get a hint button on the challenge and there are alternative solutions you can compare yours to. Also, you can probably search older posts using the forum search feature or google the challenge name and find more there.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

1 Like

thank you @ilenia, i admit my mistake, and i appreciate your advice.