Copying Array Using the Spread Operator

Hi to all,

I have some difficulties understanding the spread operator(or maybe the problem is the scope of variables…), for example if i write a function to copy an array into another.

In the last log, arr2 is empty and also, VS Code suggests me that arr2 in function args is declared but its value is never read

What is the reason ?

Thank you!
Your code so far


const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2 = [];
let copyArr = function (arr1,arr2){ // arr2 is declared but its value is never read (??)    
   "use strict";
    arr2 = [...arr1]; 
};
copyArr(arr1,arr2);
console.log(arr2);  // []

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-the-spread-operator-to-evaluate-arrays-in-place

I could be wrong, but here’s what I think is happening. Your function parameters are named arr1 and arr2, same as global variables. So, your function gets 2 local variables, named arr1 and arr2. When you call this function and the arr2 = [...arr1]; line gets executed it is not the global arr2 that gets assigned a new value, but your local arr2 variable (you can test this by logging arr2 inside your function). Check out “scope chain” if you want more info on how that works.

I think because you are misunderstanding arr2 in let arr2 = [] with arr2 in copyArr. They are different.
arr2 in copyArr is local variable in that function scope.

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2 = [];
let copyArr = function (arr1,arr2){
    "use strict";
    arr2 = [...arr1]; 
};
copyArr(arr1,arr2);
// when run this function
// parameter arr2 (in the function) will point to [...arr1]
// but variable arr2 still point to []

console.log(arr2);  // []

you could use this

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2 = [];
let copyArr = function (arr1){    
   "use strict";
    arr2 = [...arr1]; 
};
copyArr(arr1);
console.log(arr2)

but why don’t use the simple way?

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2 = [...arr1];
console.log(arr2)
2 Likes

Usually, with these exercises, the rules associated with whether you pass or fail something are specific around the particlaur way the question is set up… i.e. there may be suitable ‘other ways’ to solve the problem, but the test cases that are run when you submit it only test against one way…

so look at what fCC gives you when you reach the question ‘fresh’:

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;
(function() {
  "use strict";
  arr2 = []; // change this line
})();
console.log(arr2);

they only want you to change one line. Not to add a variable that represents the function and to call the function

only add what’s needed to pass the particular example

ie. change only the lines indicted

arr2 = []; // change this line

I did it as such for a solution and it passed…

Edit: with the new design for fCC alternate solutions work, but what I am pointing out is you don’t always have to add to much more… for instance the ‘simple solution’ above from @uybinh works as well

So the scope of the variables was the problem.
I know that i’m ovecomplicating the problem, but it’s only because didn’t understand the reason why the array was empty. Passing tests it’s not my final goal. Learn from you all it is :wink: thanks again