..Copy an Array with the Spread Operator

Tell us what’s happening:

can anyone please tell me what’s wrong with my code and point me in the right direction ?

Your code so far


function copyMachine(arr, num) {
  let newArr = [];
  while (num >= 1) {
    // change code below this line
    newArr = [...arr] * num ;
    // 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/75.0.3770.142 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/copy-an-array-with-the-spread-operator

* is for numbers, you can’t multiply something that isn’t a number. What you’re doing here doesn’t really make sense.

If you use what you have: [...arr] is indeed going to create a new copy of arr. But you are supposed to putting n copies of arr into another array, so you need to initialise that other array, on each iteration of the loop put a copy of arr into it, then return the now-full array once you’re finished

1 Like