Copy an Array with the Spread Operator...arr

sorry but i still can not understand the difference between using push(arr) , and push([…arr]) , i see the same output in the example you mentioned

Try to use both of those in this tool and see the difference

http://pythontutor.com/javascript.html

One create a copy, the other a reference

1 Like

The test case for this lesson seems to be incorrect. If you run the given code:

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

The test results says:
arr2 is correct copy of arr1 .

If you read it as “should be”, then it is clearer. Some tests are worded in a confusing way, but there it shows what you are not passing and that means that arr2 is not a correct copy of arr1

I also don’t understand the difference between […arr] and (arr), I used the tutor made changes to the code but I can’t see any difference between them, I also used it on the browser, same return…no difference

let arr = [0,0,0]
let newArr = [];
newArr.push(arr)
newArr.push([...arr])

// now newArr is [[0,0,0],[0,0,0]]
// let's make a change to arr...
arr[1] = 1;
console.log(arr) // [0,1,0]
console.log(newArr) // [[0,1,0], [0,0,0]]

when you push just arr then newArr will hold a reference - any change to arr will be reflected in the subarray of newArr
if you push a copy, like you do with [...arr] that’s a new array not connected to the starting arr

1 Like

For those still not get the difference between
newArr.push(arr); & newArr.push([…arr])
imagine you want to conduct a paper test(arr) for 10 student(num).
see newArr.push(arr); as making 10 copies using a photocopy machine. if you alterate the original copy…and start printing again, you changes will reflect on the photocopies.

see newArr.push([…arr]); as using a scanner, if you scan an original copy and you start printing… making changes to the hard copy in you hand does not affect the scanned copy.

I hope my story helps…smile

i have this one line answer working

const arr1 = ['JAN', 'FEB', 'MAR', 'APR', 'MAY'];
let arr2;

arr2 = [...arr1];  // Change this line

console.log(arr2);

arr2 = […arr1];

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.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

1 Like

Sorry for that!
I will take care of this for sure from next time.
That was unprofessional of me sharing solution instead encouraging people to learn and explore.
Thanks for marking it!