Move an item to a new position
Drag a task from the bottom of a list to the top and something has to compute the new order. Doing it with splice on the state array directly is how you get a list that reorders in memory but never re-renders.
Implement move(items, from, to). Take the item at index from, remove it, and re-insert it so that it ends up at index to in the returned array. Both indices may be negative, counting back from the end, so -1 refers to the last item. Return a new array; the input is never modified. If either index, after resolving a negative value, falls outside the range of valid positions in the array, return an unchanged shallow copy of the input.
What it has to do
- Return a new array with the item relocated, leaving the other items in order.
- Treat negative indices as counting back from the end, so
-1is the last item. - Return an unchanged shallow copy when either resolved index is out of range.
- Handle moving both forwards and backwards through the array.
- Do not mutate the input array.
Your workspace
Ready to check it?
5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.
AI Crack & Solution Assist
Stuck? Get instant AI hints or break down the optimal solution.
Stuck? The javascript course covers everything this challenge needs.