Last active
March 6, 2020 04:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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