What Other Function?

It’s as the title says. What other function is the hint given in the challenge talking about? Could I at least get a hint?

My code so far


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

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/83.0.4103.106 Safari/537.36 Edg/83.0.478.54.

Challenge: Copy an Array with the Spread Operator

Link to the challenge:

I don’t understand the question. What do you mean by “other function”?

The specs for the challenge are:

We have defined a function, copyMachine which takes arr (an array) and num (a number) as arguments. The function is supposed to return a new array made up of num copies of arr. We have done most of the work for you, but it doesn't work quite right yet. Modify the function using spread syntax so that it works correctly (hint: another method we have already covered might come in handy here!).

Notice the hint. hint: another method we have already covered might come in handy here! The question I’m asking is about the method hinted at here.

Concerning the hint, do you remember how to add something to the end of an array? That’s what the hint is referring to.

When I do

newArr.push(...arr);

I get

[ true, false, true, true, false, true ]

which won’t pass the challenge. I can get the expected return value by doing

newArr.push(arr);

but that doesn’t make use the spread operator. What am I missing?

You can’t pass the challenge by using

newArr.push(arr);

I just tried it and it failed. You are however very very close to the correct solution with your first attempt:

newArr.push(...arr);

Look at the examples again:

let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];

See how they are creating a copy of the array? Each element you push onto newArr should be a copy of arr.