Need help to understand array assignment on this challenge

Tell us what’s happening: Hi, first time posting, so feel free to correct me in anyways
So, this is the solution, but if I change the line newArr.push([…arr]); to newArr = newArr.push, I get an error saying: TypeError: newArr.push is not a function

So, what is wrong about my way of thinking? I see that I can’t assign the value of an array that way, but I don’t understand why! Thanks for your help!

Your code so far


function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // change code below this line
    newArr.push([...arr]);
    // change code above this line
    num--;
  }
  return newArr;
}

// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));

Your browser information:

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

Challenge: Copy an Array with the Spread Operator

Link to the challenge:

push returns the new length of the array, so after that the code is trying to use array methods on a number and it results in an error

Yeah, I am always forgetting which methods modify the original and which ones return what. It’s good to get in the habit of checking MDN when you’re not sure. Here is the entry for push.

thanks for your reply!! I thought I was creating a new array, but now I understand that it is just a reference to the original one and that’s when I got the error you explained!