Question - freeCodeCamp Challenge Guide: Slice and Splice

Sorry, but i dont know even what to google to find answers to my question.

I was doing this challenge freeCodeCamp Challenge Guide: Slice and Splice

With solution:

let superArr = [4,5,6];
function frankenSplice(arr1, arr2, n) {
  arr2.splice(n,0, ...arr1)
  let hero = [];
  for (let i=0 ; i<arr2.length; i++)
  { hero.push(arr2[i])}
  arr2 = arr2.splice(n , arr1.length)
  console.log(arr2) //[1,2,3]
  return hero;}
frankenSplice([1, 2, 3], [superArr], 1);
console.log(superArr);            // [4,5,6] !?

One of the requirements is to “The second array should remain the same after the function runs.”

How the second array continue to remain the same if in the function it loggs like [1,2,3], but outside is working like [4,5,6].

What i should to read to understand this ?

As the guide says this and this is all you need to understand those concepts. Mdn even has the demo script which you can change and run to understand things better.

Didnt get it.
Inside the function second array loggs like [1,2,3]
Outside the function it loggs like [4,5,6]
Why?
And it continue to remain the same after function runs.

From this JavaScriptTutorial.net article (emphasis mine):

In JavaScript, function arguments are always passed by value. It means that JavaScript copies the values of the variables into the function arguments.

So changing the value(s) of the argument(s) inside the function doesn’t affect the original value.

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

To be clear, the values [1, 2, 3], [4, 5, 6], and 1 are considered the arguments for this function.

If you’re doing the JavaScript Algorithms and Data Structures learning path like I am, I don’t think JavaScript data types have really been in depth to this point in the curriculum.

Briefly, data types like String, Number, Boolean, Undefined and Null are all considered “primitive data types”, while Object, Function and Array are categorized as “reference data types”. When we create a variable name (also known as an identifier) and assign it data, that data is stored and accessed differently based on its data type categorization (primitive or reference).

I recommend reading freeCodeCamp’s article on Primitive vs Reference Data Types in JavaScript.

1 Like

Your answer was very useful. Thank you much for information and sharing!

1 Like

Simple and illustrative for beginners:

Mod Edit: 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.

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.

Noted with thanks. Actually new here.

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