Spread Operator Challenge. Help!

Good day friends!
After checking the Hint to this challenge I passed the challenge but to be honest, I don’t quiet understood the logic behinde the solution.
I expected to see something like this


function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
  // Only change code below this line
  //num == arr;
  num = [...arr];  // Since I was to copy arr and make it num
  newArr.push(num);  // then I pushed  num in to the newArr

  // Only change code above this line
  num--;
}
return newArr;
}

console.log(copyMachine([true, false, true], 2));

Please someone should help me with the explanation of the solution .
Why do we need to declare a new variable to hold the copy?
Thanks!

  **Your browser information:**

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

Challenge: Copy an Array with the Spread Operator

Link to the challenge:

no, you are overwriting a function parameter

you can’t do num-- now that num is an array

1 Like

Thanks!
Here is the correct solution to the challenge.

function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // Only change code below this line
    //num == arr;
    let newNum = [...arr]; // Why do I need this line?
    newArr.push(newNum);

    // Only change code above this line
    num--;
  }
  return newArr;
}

console.log(copyMachine([true, false, true], 2));

But I don’t understand the logic completely

What would you put instead of the line you don’t understand?


I added spoiler tags to the full working soultion code.

Thanks!
I just need an explanation about the line.
Why we need to declare another variable?

What happens when you try

let smallArr = [1, 2, 3];
let bigArr = [smallArr, smallArr];

bigArr[0][1] = 42;
console.log(bigArr);

Index 1 will be replaced by 42.
I still don’t get the point yet

try it, what does it print to the console?

1 Like

This is what it logged
[ [ 1, 42, 3 ], [ 1, 42, 3 ] ]

Honestly this isn’t what I expected also. I’m confused even more now.

I expected it to log
[ [ 1, 42, 3 ], [ 1, 2, 3 ] ]

that’s why you need to make a copy

you don’t need a new variable tho
you can do push([...arr])

2 Likes

This is exactly why you need a new variable.

In Javascript, roughly speaking, a variable can only hold one thing. If you want to hold an entire array, then Javascript puts the array in your computer’s memory and then stores the location of the array in your variable.

With

let myArray = [1, 2, 3];
let myNewArray = myArray;

both variables point to the same place in memory.

With

let myArray = [1, 2, 3];
let myNewArray = [...myArray];

the spread operator makes a copy the contents (top level contents, really) of the first array, then the []s mean the contents are stored in a new place for an array in memory, and then that new location for the copied array is stored to myNewArray.

1 Like

Thanks Guys!
I got the logic. Thanks a lot.
I really appreciate your time.
Thanks!

1 Like