--- id: 587d7b7b367417b2b2512b13 title: Copy an Array with the Spread Operator challengeType: 1 forumTopicId: 301157 localeTitle: Скопируйте массив с помощью оператора распространения --- ## Description
Хотя slice() позволяет нам выбирать, какие элементы массива копировать, среди нескольких других полезных задач, новый оператор с расширением ES6 позволяет нам легко скопировать все элементы массива в порядке, с простым и хорошо читаемым синтаксисом. Синтаксис распространения просто выглядит так: ... На практике мы можем использовать оператор спредов для копирования массива следующим образом:
let thisArray = [true, true, undefined, false, null];
let thatArray = [... thisArray];
// thisArray равно [true, true, undefined, false, null]
// thisArray остается неизменным и идентичен этому массиву
## Instructions
Мы определили функцию copyMachine которая принимает arr (массив) и num (число) в качестве аргументов. Функция должна возвращать новый массив, состоящий из num копий arr . Мы выполнили большую часть работы для вас, но пока это не работает. Измените функцию, используя синтаксис распространения, чтобы он работал правильно (подсказка: здесь может быть полезен другой метод, который мы уже рассмотрели!).
## Tests
```yml tests: - text: copyMachine([true, false, true], 2) should return [[true, false, true], [true, false, true]] testString: assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]]); - text: copyMachine([1, 2, 3], 5) should return [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]] testString: assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]); - text: copyMachine([true, true, null], 1) should return [[true, true, null]] testString: assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]); - text: copyMachine(["it works"], 3) should return [["it works"], ["it works"], ["it works"]] testString: assert.deepEqual(copyMachine(['it works'], 3), [['it works'], ['it works'], ['it works']]); - text: The copyMachine function should utilize the spread operator with array arr testString: assert(removeJSComments(code).match(/\.\.\.arr/)); ```
## Challenge Seed
```js function copyMachine(arr, num) { let newArr = []; while (num >= 1) { // change code below this line // change code above this line num--; } return newArr; } // change code here to test different cases: console.log(copyMachine([true, false, true], 2)); ```
### After Tests
```js const removeJSComments = str => str.replace(/\/\*[\s\S]*?\*\/|\/\/.*$/gm, ''); ```
## Solution
```js 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; } console.log(copyMachine([true, false, true], 2)); ```