freeCodeCamp Challenge Guide: Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements

Use Destructuring Assignment with the Rest Parameter to Reassign Array Elements


Problem Explanation

Remember that the rest parameter allows for variable numbers of arguments. In this challenge, you have to get rid of the first two elements of an array.


Hints

Hint 1

Assign the first two elements to two random variables.

Hint 2

Set the remaining part of the array to ...shorterList.


Solutions

Solution 1 (Click to Show/Hide)
function removeFirstTwo(list) {
  // Only change code below this line
  const [a, b, ...shorterList] = list; // Change this line
  // Only change code above this line
  return shorterList;
}

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

You can also exclude the first two elements of the arr array using ,,.

Relevant Links

Solution 2 (Click to Show/Hide)
function removeFirstTwo(list) {
  // Only change code below this line
  const [, , ...shorterList] = list; // Change this line
  // Only change code above this line
  return shorterList;
}

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