Move Array Element
Prompt
Write a function move that takes an array, a position index, and a shift amount, then moves the element at that position by the given number of places.
- Negative
posvalues count from the end of the array (e.g.,-1is the last element). - The shift amount moves the element forward (positive) or backward (negative).
- If the new position goes out of bounds, it should wrap around to the other side of the array.
- The function should mutate and return the original array.
If pos is negative, convert it to a positive index using length + pos. For example, -1 in a 5-element array becomes index 4.
Use splice() twice: once to remove the element from its current position, and once to insert it at the new position. Remember that splice returns an array, so destructure to get the single element.
When the target index goes out of bounds, the double-modulo trick ((index % length) + length) % length ensures it wraps around correctly, even for negative values. JavaScript's % operator can return negative numbers, which is why you need the extra + length step.
Solution
Explanation
This problem looks simple at first, but the edge cases around negative indices and wrapping are where most candidates stumble.
The first step is normalizing the starting position. If pos is negative, we convert it to a positive index by adding the array's length. So in a 5-element array, -1 becomes 4 (the last element), and -2 becomes 3 (the second-to-last). This mimics how languages like Python handle negative indexing.
Next, we calculate where the element should land: fromIndex + shift. But this raw target can easily fall outside the array bounds. A shift of +6 on a 5-element array would give us index 6, which doesn't exist. A negative shift could push us below zero. We need wrapping.
The double-modulo formula ((toIndex % length) + length) % length handles this. It's a common pattern and worth memorizing. The reason for the extra steps is that JavaScript's remainder operator % can return negative values. For example, -1 % 5 gives -1, not 4. By adding length first and taking modulo again, we guarantee a positive result within bounds.
With the indices sorted out, the actual move is two splice calls. The first one, arr.splice(fromIndex, 1), removes the element and returns it inside an array. We destructure with [element] because splice always returns an array, even for a single removal. The second call, arr.splice(toIndex, 0, element), inserts the element at the target position without removing anything (that's what the 0 argument means).
One thing to be aware of: when you splice an element out of the array, all elements after it shift left to fill the gap. This means the indices change. Fortunately, because we compute toIndex before removing anything, and splice handles shifting automatically when inserting, everything lines up correctly. But if you tried to do the insert first and remove second, you'd get the wrong result because the indices would be off.
The Double Modulo Trick
In JavaScript, -1 % 5 returns -1, not 4. This is different from Python, where -1 % 5 gives 4. The formula ((n % len) + len) % len corrects for this by shifting the result into positive territory before the final modulo. It's useful anytime you need circular/wrap-around behavior with arrays.