Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements

Sincerly, I do not know what to do https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-with-the-rest-parameter-to-reassign-array-elements


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

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 it easier to read.

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

Note: Backticks are not single quotes.

markdown_Forums

Look at the example code and see how they were able to achieve an effect similar to splice() using destructuring. You’ll want to use that same principle to pass the challenge.

1 Like
I have to finish only an objective.

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



// 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];

that’s kinda cheating
you need to use destructuring and the ... rest parameter

look at the example code

1 Like

I do not understand the problem with this.

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


you are overwriting source and arr is undefined

look at the example code for how destructuring and rest parameter are to be used

const [a, b, ...arr] = [1, 2, 3, 4, 5, 7];
console.log(a, b); // 1, 2
console.log(arr); // [3, 4, 5, 7]

if you do not understand the example code please ask

1 Like

May you help me with another example, please?

first, do you understand array destructuring?

let [color, shape] = ["red", "square"]
console.log(color); // "red"
console.log(shape); // "square"

and the rest parameter?

function myFunc (a, ...args) {
  return args;
}
console.log(myFunc(5,8,7,3)); // [8,7,3]

if you understand them then it is easy to combine them, if you don’t you need to research and try till you understand them

play with the code, try things out till you understand them

2 Likes

What is the reason why it is not possible to do this?

const source = [1,2,3,4,5,6,7,8,9,10];

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


in JavaScript you assign what’s to the right of the assignment operator to what is to the left of it - you are trying to do the opposite, it will just gives errors

and then this is wrong syntax:
source[1,2,3,4,5,6,7,8,9,10]

you need to follow the rules

1 Like

How can make it correctly?

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]=[1,2,3,4,5,6,7,8,9,10];


  // 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];


and what happens if we call removeFirstTwo with a different argument?
like

const arr = [101, 100, 99, 98, 97, 96]
const numbersLessThan100 = removeFirstTwo(arr);
console.log(numbersLessThan100); // should be [99, 98, 97, 96]
2 Likes

It does not work because it is working with list and some element wh
ich it is inside of it

So, in the above function you posted, you are assigning a literal array ([1,2,3,4,5,6,7,8,9,10]) to those const variables. The issue here is, there is a parameter being passed into your function. If you take a look in the first line of the quoted code, you see what is known as a function signature. That’s the function’s name, and what parameters it receives. Your function’s name is removeFirstTwo, and what is the parameter?

The big question is, are you using that parameter anywhere?

1 Like

No, I am not using it :frowning: . But, I am trying const arr = list; and it is not working yet.:frowning:

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]=[1,2,3,4,5,6,7,8,9,10];

const list = arr;

  // 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 const [a, b, ...arr] was working fine, but why not try using list with that?

When you do const arr = list;, what you’ve done is create an reference to that outer array object. Instead, you still need to set a, and b, and arr equal to the parts of list that you want.

Get rid of the hard-coded [1,2,3,4,5,6,7,8,9,10], that’s just making the whole thing break. You are being passed a list, some array, and you’re being asked to remove the first two members of that array using destructuring. Right now, you’re destructuring on that literal, hard-coded array. What happens if you use list there instead of completely typing out a fixed array?

1 Like

I do not undestand, could you give an example to me, please?