Basic Data Structures: Copy an Array with the Spread Operator question


I have this code:

function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // change code below this line
    newArr += [...arr];
    // change code above this line
  return newArr;
// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));

console log prints true,false,truetrue,false,true

Any ideas?

Can you be more specific with your question? What is behaving differently than you expect? What else have you tried.

What do you think this is doing?

It adds arrays to the main array.

I should get these results:

copyMachine([true, false, true], 2)

should return

[[true, false, true], [true, false, true]]
copyMachine([1, 2, 3], 5)

should return

[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
copyMachine([true, true, null], 1)

should return

[[true, true, null]]
copyMachine(["it works"], 3)

should return

[["it works"], ["it works"], ["it works"]]

Instead I get this: true,false,truetrue,false,true

That is not how you add an element to an array. You should probably review the following challenge:

Manipulate Arrays With push()

Thank you for your replies. Push worked.