diff --git a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md index 7378ed99bd..4d04428c44 100644 --- a/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md +++ b/guide/english/certifications/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements/index.md @@ -2,7 +2,7 @@ title: Use Destructuring Assignment with the Rest Operator to Reassign Array Elements --- ## Use Destructuring Assignment with the Rest Operator to Reassign Array Elements - + Remember that the rest operator allows for variable numbers of arguments. In this challenge, you have to get rid of the first two elements of an array. ## Hint 1: @@ -13,10 +13,9 @@ Assign the first two elements to two random variables. Set the remaining part of the array to `...arr`. -======= -## Hint 1 +## Hint 3: -Use destructuring to create the `arr` variable. +Use destructuring to create the `arr` variable: ```javascript function removeFirstTwo(list) { @@ -28,9 +27,9 @@ function removeFirstTwo(list) { } ``` -## Hint 2 +## Hint 4: -Spread the `list` parameter into `arr`. +Spread the `list` parameter values into `arr`. ```javascript function removeFirstTwo(list) { @@ -42,11 +41,29 @@ function removeFirstTwo(list) { } ``` -## Hint 3 -Exclude the first two elements of the `arr` array with `,,`. +## Spoiler Alert - Solution Ahead! +You can use random variables to omit the first two values: ```javascript +const source = [1,2,3,4,5,6,7,8,9,10]; +function removeFirstTwo(list) { +"use strict"; + // change code below this line + const [a, b, ...arr] = list; + // change code above this line + return arr; +} +const arr = removeFirstTwo(source); +console.log(arr); // should be [3,4,5,6,7,8,9,10] +console.log(source); // should be [1,2,3,4,5,6,7,8,9,10]; +``` +## Solution 2: + +You can also exclude the first two elements of the `arr` array using `,,`. + +```javascript +const source = [1,2,3,4,5,6,7,8,9,10]; function removeFirstTwo(list) { "use strict"; // change code below this line @@ -54,16 +71,11 @@ function removeFirstTwo(list) { // change code above this line return arr; } +const arr = removeFirstTwo(source); +console.log(arr); // should be [3,4,5,6,7,8,9,10] +console.log(source); // should be [1,2,3,4,5,6,7,8,9,10]; ``` -## Spoiler Alert - Solution Ahead! +### Resources -```javascript -function removeFirstTwo(list) { - "use strict"; - // change code below this line - const [a, b, ...arr] = list; - // change code above this line - return arr; -} -``` +- ["Destructuring assignment" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment)