ES6 - Mutate an Array Declared with const

Tell us what’s happening:

I know it’s simple using index to assign new variable, but I want to try another way using pop and push method. can someone please fix the code so It can work as well for this challenge?

Your code so far

const s = [5, 7, 2];

function editInPlace() {
//  i want to use .pop and .push for this challenge. please help

  const delLast = s.pop();
  return s.push(2);

  // or can it be ? 
  // return s.pop().push(2)   ?
}
editInPlace();

Your browser information:

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

Challenge Information:

ES6 - Mutate an Array Declared with const

after removing the last element using pop() , use unshift() to add this element back to the beginning of the array

Push puts stuff at the end of the array. Where does pop remove from?

You would have to pop all the elements and add them back in the correct order.

You can’t chain the methods because they do not return this.

pop returns the popped element and push returns the new array length. But you can compose them.

const reverse = [3, 2, 1];
reverse.push(reverse.pop(), reverse.pop(), reverse.pop());
console.log(reverse); // [1, 2, 3]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.