* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2.2 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b8a367417b2b2512b4c | Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements | 1 | 301218 | 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:
const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b);
console.log(arr);
The console would display the values 1, 2
and [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]
assert(arr.every((v, i) => v === i + 3) && arr.length === 8);
source
should be [1,2,3,4,5,6,7,8,9,10]
assert(source.every((v, i) => v === i + 1) && source.length === 10);
Array.slice()
should not be used.
(getUserInput) => assert(!getUserInput('index').match(/slice/g));
Destructuring on list
should be used.
assert(
__helpers
.removeWhiteSpace(code)
.match(/\[(([_$a-z]\w*)?,){1,}\.\.\.arr\]=list/i)
);
--seed--
--seed-contents--
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--
const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
const [, , ...arr] = list;
return arr;
}
const arr = removeFirstTwo(source);