Files

47 lines
1.1 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Copy an Array with the Spread Operator
---
# Copy an Array with the Spread Operator
2018-10-12 15:37:13 -04:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
- The final hint in the example tells you to use a recently learned method.
### Hint 2
2018-10-12 15:37:13 -04:00
- The spread operator copies all elements into a new empty object.
```javascript
while (num >= 1) {
newArr = [...arr]
num--;
}
```
- The code above will copy all of the elements into `newArr` but will also reinitialise `newArr` with every new iteration of the while loop.
- A new variable should first be initialised using the spread operator - `let obj = [...arr];` - then this variable should be added to the `newArr` for every iteration of the while loop.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
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;
}
// change code here to test different cases:
console.log(copyMachine([true, false, true], 2));
```
</details>