How can I get the remained part without creating a variable?

we use Array.splice() to get the part of spliced, I wonder how I can get the remained part, without create another variable and let it = Array.splice().

Your code so far

const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
arr.splice(1, 4);
// Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36

Challenge: Remove Items Using splice()

Link to the challenge:

splice returns the removed element, so the output of splice is the spliced ones, the mutated array is what is not unspliced

that’s how splice work - if you don’t want to mutate arr you need to use other methods

Yep, what I mean is how I can get the remained part, you have any idea?

1 Like
const arr = [1,2,3,4,5,6,7]
const removed = arr.splice(1, 4)
console.log(removed); //array with the removed elements
console.log(arr); // array with remaining elements
1 Like

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