Use Destructuring Assignment with the Rest Operator

Use Destructuring Assignment with the Rest Operator
0

#1

Tell us what’s happening:

Again stuck with destructuring.

Your code so far


const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
  "use strict";
  // change code below this line
  const {list:arr}= [3,4,5,6,7,8,9,10] // change this
  // 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];

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-operator-to-reassign-array-elements


#2

You have several issues going on.

#1) You are destructuring an array and not an object, so you would not write the following.

const {list:arr}=

Your declaration will look more like:

const [ ?????????????????? ] =

#2) You are supposed to be using the spread operator ..., but I do not see where you are doing that.

#3) Why are you using a hardcoded array [3,4,5,6,7,8,9,10] , when you should be referencing the array passed into the function represented by the list parameter.


#3

Is it ok?

const source = [1,2,3,4,5,6,7,8,9,10];
function removeFirstTwo(list) {
  "use strict";
  // change code below this line
  const [...arr]= source; // change this
  // change code above this line
  return arr;
}

#4

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

You are still not referencing the argument passed into the function for the array on the right side of the =. You are using the global variable source.

Also, const […arr] creates a copy of the entire array which still includes the element values 1 and 2. How could you capture the 1 and 2 so that …arr and array containing the remaining elements 3, 4, 5, 6, 7, 8, 9, 10?

HINT: The challenge example shows you exactly how to do this.


#5

U are a beauty…thanks for the hints… i sot it out.