ES6 - Destructuring via rest elements

Tell us what’s happening:
Describe your issue in detail here.
How do I get my console.log(c,d,e) to equal removeFirstTwo();

Your code so far

function removeFirstTwo(list) {
  return list;
}

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

const [a,b,c,d,e, ...arr] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log(c,d,e);

Your browser information:

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

Challenge: ES6 - Destructuring via rest elements

Link to the challenge:

I found this article to be very helpful:

You want to create a variable with the first to elements and then three dots and the rest to equel your list:

const [firstElement,secondElement,...theRest] = variable;
return theRest;

basicly if variable is [3,4,4,5,6]
Then firstElement is equel to 3.
secondElement is equal to 4.
And the rest of your elements in the array are assigned to the variable ‘theRest’.
Hope this helps.

This was helpful but I am still misunderstanding something because I haven’t gotten the correct answer. When I type const [, ,c,d,e,.arr] outside of the function and type console.log(c,d,e) I get 3,4,5 but if I use the information from the hint and place it inside the function I do not get the correct answer. What am I not seeing?

function removeFirstTwo(list) {
const [, ,c,d,e, …arr] = list;
return list;
}

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

const sourceWithoutFirstTwo = removeFirstTwo(source);

You are only meant to skip the first two elements. You are doing that using empty elements [, ,] the rest of the identifiers you added c,d,e will remove three more elements which you are not supposed to do.

const [, , ...theRest] = [8, 9, 1, 2, 3, 4, 5]
console.log(theRest) // [1, 2, 3, 4, 5]
const [, , a, b, c, ...theRest] = [8, 9, 1, 2, 3, 4, 5]
console.log(theRest) // [4, 5]
1 Like

Thank you for your help but I am not passing. I tried
const [, , …theRest] = [8, 9, 1, 2, 3, 4, 5]
and
const[, , …theRest] = [1,2,3,4,5]

The code looks like this

function removeFirstTwo(list) {
const [, , …theRest] = [8, 9, 1, 2, 3, 4, 5]
return list;
}

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

You want the variables:

[,,...theRest]

To be set to your list argument and not :

[8, 9, 1, 2, 3, 4, 5].

Also don’t return the list argument only the variable theRest should be returned:

I want the variable 3,4,5 but nothing I am plugging is working.

I am not sure what you mean. I tried.
function removeFirstTwo(list) {
const [, , …theRest] = [3, 4, 5]
return list;
}

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

and

function removeFirstTwo(list) {
const [, , …theRest] = [1,2, 3, 4, 5]
return list;
}

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

Hold up a second.

Use a destructuring assignment with the rest syntax to emulate the behavior of Array.prototype.slice(). removeFirstTwo() should return a sub-array of the original array list with the first two elements omitted.

Ok, what are you supposed to return? It will not be the original list since destructuring does not mutate (change) the original array.

These are the instructions. I can’t changed (list) or use array.prototype.slice().

Use a destructuring assignment with the rest syntax to emulate the behavior of Array.prototype.slice() . removeFirstTwo() should return a sub-array of the original array list with the first two elements omitted.

I just showed you the instructions. I want you to answer my question in your own words. You can’t accomplish a task if you can’t articulate what it wants!

This the paragraph provided. I think it means to removeFirstTwo(list) hast to equal 3,4,5. I used the destructuring to get to have an output of 345 but it not = removeFirstTwo(list). I am not allowed to change list or use slice.

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 syntax 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 syntax to catch a subarray that leaves out the last element of the original array.


Use a destructuring assignment with the rest syntax to emulate the behavior of Array.prototype.slice(). removeFirstTwo() should return a sub-array of the original array list with the first two elements omitted.

This is the key! If you cannot change list, then

This line can never return the list without the first two elements.

This line never involves the list, so it can’t be right either.

you can’t change the original list but you can create a new variable which contains the contents of the original list mines the first two elements.

const [,,theRest]

if equel to the original list then theRest will contain all the list elements with the first two omitted. Now return this variable instead of the original list which was not modified.

I attempted your suggestion and it didn’t pass.

What is your updated code?

I also attempted

function removeFirstTwo(list) {
const [a,b,…theRest] = [1,2,3, 4, 5];
const theRest = list;
return list;
}

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

function removeFirstTwo(list) {
const [a,b,…theRest] = [1,2,3, 4, 5];
const theRest = sourceWithoutFirstTwo;
console.log(sourceWithoutFirstTwo);
return list;
}

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

This gave me the output 3,4,5 but not 3,4,5 connected to the function

function removeFirstTwo(list) {
const [a,b,…theRest] = [1,2,3, 4, 5];
console.log(theRest);
return list;
}

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

You seem to not understand what variables do.

You probably need to go back and review the lessons involving variables and functions because guessing random syntax won’t help here

I figured out the problem. Thanks everyone.

2 Likes