* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
85 lines
2.1 KiB
Markdown
85 lines
2.1 KiB
Markdown
---
|
|
id: 587d7b8a367417b2b2512b4c
|
|
title: >-
|
|
Use Destructuring Assignment with the Rest Parameter to Reassign Array
|
|
Elements
|
|
challengeType: 1
|
|
forumTopicId: 301218
|
|
dashedName: >-
|
|
use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements
|
|
---
|
|
|
|
# --description--
|
|
|
|
In some situations involving array destructuring, we might want to collect the rest of the elements into a separate array.
|
|
|
|
The result is similar to `Array.prototype.slice()`, as shown below:
|
|
|
|
```js
|
|
const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
|
|
console.log(a, b); // 1, 2
|
|
console.log(arr); // [3, 4, 5, 7]
|
|
```
|
|
|
|
Variables `a` and `b` take the first and second values from the array. After that, because of the rest parameter's presence, `arr` gets the rest of the values in the form of an array. The rest element only works correctly as the last variable in the list. As in, you cannot use the rest parameter to catch a subarray that leaves out the last element of the original array.
|
|
|
|
# --instructions--
|
|
|
|
Use destructuring assignment with the rest parameter to perform an effective `Array.prototype.slice()` so that `arr` is a sub-array of the original array `source` with the first two elements omitted.
|
|
|
|
# --hints--
|
|
|
|
`arr` should be `[3,4,5,6,7,8,9,10]`
|
|
|
|
```js
|
|
assert(arr.every((v, i) => v === i + 3) && arr.length === 8);
|
|
```
|
|
|
|
`source` should be `[1,2,3,4,5,6,7,8,9,10]`
|
|
|
|
```js
|
|
assert(source.every((v, i) => v === i + 1) && source.length === 10);
|
|
```
|
|
|
|
`Array.slice()` should not be used.
|
|
|
|
```js
|
|
(getUserInput) => assert(!getUserInput('index').match(/slice/g));
|
|
```
|
|
|
|
Destructuring on `list` should be used.
|
|
|
|
```js
|
|
assert(
|
|
__helpers
|
|
.removeWhiteSpace(code)
|
|
.match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i)
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
const source = [1,2,3,4,5,6,7,8,9,10];
|
|
function removeFirstTwo(list) {
|
|
// Only change code below this line
|
|
const arr = list; // Change this line
|
|
// Only change code above this line
|
|
return arr;
|
|
}
|
|
const arr = removeFirstTwo(source);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
const source = [1,2,3,4,5,6,7,8,9,10];
|
|
function removeFirstTwo(list) {
|
|
const [, , ...arr] = list;
|
|
return arr;
|
|
}
|
|
const arr = removeFirstTwo(source);
|
|
```
|