This includes certificates (where it does nothing), but does not include any translations.
2.4 KiB
2.4 KiB
id, title, challengeType, isHidden, forumTopicId
id | title | challengeType | isHidden | forumTopicId |
---|---|---|---|---|
587d7b8a367417b2b2512b4c | Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements | 1 | false | 301218 |
Description
Array.prototype.slice()
, as shown below:
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
Array.prototype.slice()
so that arr
is a sub-array of the original array source
with the first two elements omitted.
Tests
tests:
- text: <code>arr</code> should be <code>[3,4,5,6,7,8,9,10]</code>
testString: assert(arr.every((v, i) => v === i + 3) && arr.length === 8);
- text: <code>source</code> should be <code>[1,2,3,4,5,6,7,8,9,10]</code>
testString: assert(source.every((v, i) => v === i + 1) && source.length === 10);
- text: <code>Array.slice()</code> should not be used.
testString: getUserInput => assert(!getUserInput('index').match(/slice/g));
- text: Destructuring on <code>list</code> should be used.
testString: assert(code.replace(/\s/g, '').match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i));
Challenge Seed
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
// Only change code below this line
const arr = list; // Change this line
// Only change code above this line
return arr;
}
const arr = removeFirstTwo(source);
Solution
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
"use strict";
const [, , ...arr] = list;
return arr;
}
const arr = removeFirstTwo(source);