2018-09-30 23:01:58 +01:00
---
id: 587d7b7b367417b2b2512b13
title: Copy an Array with the Spread Operator
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301157
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
While `slice()` allows us to be selective about what elements of an array to copy, among several other useful tasks, ES6's new < dfn > spread operator</ dfn > allows us to easily copy *all* of an array's elements, in order, with a simple and highly readable syntax. The spread syntax simply looks like this: `...`
2018-09-30 23:01:58 +01:00
In practice, we can use the spread operator to copy an array like so:
2019-04-26 17:21:49 -07:00
2019-05-01 09:33:02 -07:00
```js
2019-04-26 17:21:49 -07:00
let thisArray = [true, true, undefined, false, null];
let thatArray = [...thisArray];
// thatArray equals [true, true, undefined, false, null]
2020-10-15 04:53:49 +03:00
// thisArray remains unchanged and thatArray contains the same elements as thisArray
2019-04-26 17:21:49 -07:00
```
2020-11-27 19:02:05 +01:00
# --instructions--
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!).
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`copyMachine([true, false, true], 2)` should return `[[true, false, true], [true, false, true]]`
```js
assert.deepEqual(copyMachine([true, false, true], 2), [
[true, false, true],
[true, false, true]
]);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`copyMachine([1, 2, 3], 5)` should return `[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(copyMachine([1, 2, 3], 5), [
[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]]`
```js
assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]]);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`copyMachine(["it works"], 3)` should return `[["it works"], ["it works"], ["it works"]]`
```js
assert.deepEqual(copyMachine(['it works'], 3), [
['it works'],
['it works'],
['it works']
]);
```
The `copyMachine` function should utilize the `spread operator` with array `arr`
```js
assert(__helpers.removeJSComments(code).match(/\.\.\.arr/));
```
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function copyMachine(arr, num) {
let newArr = [];
while (num >= 1) {
2020-03-02 23:18:30 -08:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
2020-03-02 23:18:30 -08:00
// Only change code above this line
2018-09-30 23:01:58 +01:00
num--;
}
return newArr;
}
console.log(copyMachine([true, false, true], 2));
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-05-01 01:37:27 +02:00
function copyMachine(arr,num){
2020-11-27 19:02:05 +01:00
let newArr=[];
while(num >=1){
newArr.push([...arr]);
num--;
}
return newArr;
2019-05-01 01:37:27 +02:00
}
console.log(copyMachine([true, false, true], 2));
2018-09-30 23:01:58 +01:00
```