Sure. It’s a little deceptive, though. Almost more an object thing than an array. But here’s the logic:
- Given two index numbers and an array, this is a mechanism to exchange them.
- First, store the element at the first index to a variable. This way we don’t lose it.
- Next, move the array element at the second index into that first index position.
- Last, that value we stuck in a variable? It replaces the old value at that second index.
Interestingly, if you call it with two property names and an object, like so:
. swap(“firstName”, “lastName”, studentRecordObject):
… It would still work.