Drop it Code Discussion, (Spoiler: Answer Ahead)

Tell us what’s happening:
Hey guys, I want to know why my code doesn’t work if I change line 3 to
let newArr = arr;
(Please refer to line 3)

Your code so far

function dropElements(arr, func) {
  // Drop them elements.
  let newArr = [...arr]; // The code doesn't work if I change this to let newArr = arr
  for (let i = 0; i < arr.length; i++) {
    if (!func(arr[i])) {
    } else {
  return newArr;

dropElements([1, 2, 3], function(n) {return n < 3; });

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:

when you do newArr = arr, whatever you do to newArr will affect arr because you have made a soft copy. When you make a soft copy, you’re not actually copying the values of arr but rather a reference to arr.

[…arr] soft copies the items in the array. If none of the elements of arr are objects (which they aren’t in the test cases), then what you do to newArr will not affect anything in arr.

1 Like

Thanks for your explanation. Really appreciate it XD