Skip to content

Instantly share code, notes, and snippets.

@donavon
Last active March 6, 2020 04:54
Show Gist options
  • Save donavon/0bb7d09c740102f8b657e52ebe30895f to your computer and use it in GitHub Desktop.
Save donavon/0bb7d09c740102f8b657e52ebe30895f to your computer and use it in GitHub Desktop.
Reverse an Array in place. Note: No need to swap middle element if there is an off number of elements.
const reverseArray = arr => {
const len = arr.length;
const halfLen = Math.floor(len / 2);
for (let i = 0; i < halfLen; i++) {
const temp = arr[i];
arr[i] = arr[len - i - 1];
arr[len - i - 1] = temp;
}
return arr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment