Slice and Splice: ?(ReferenceError: arr2 is not defined)

I thought this result should be right, but got a (ReferenceError: arr2 is not defined), anybody can help? thanks!

function frankenSplice(arr1, arr2, n) {

let resultArry=arr2.slice();

 for(let i=0; i<arr1.length; i++){

 resultArry = arr2.splice(n,0,arr1[i]);

 n++;  

}

return resultArry;

}

frankenSplice([1, 2, 3], [4, 5, 6], 1);

console.log(arr2);

So when you console.log a variable you’ve defined inside a function, but you’re trying to log it outside that function…

Javascript is telling you true. In the global scope, where you’re trying to log, there is no arr2 move that line inside the function, see what happens.

Also, you call your function, and you’re having it return something, but what do you want to do with that returned value?

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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

thanks! it’s about the " Slice and Splice".

I can see that. But this line, you are calling the function, and the function is returning something to you. You are evaluating your arrays in the context of this function, and the function is telling you something in response.

So let me ask again: what are you, in this line, doing with the returned value?

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