Skip to content

Instantly share code, notes, and snippets.

@DmitryOlkhovoi
Created December 10, 2017 12:17
Show Gist options
  • Save DmitryOlkhovoi/d4aa1a4a991bd51744f6890cd21b31f1 to your computer and use it in GitHub Desktop.
Save DmitryOlkhovoi/d4aa1a4a991bd51744f6890cd21b31f1 to your computer and use it in GitHub Desktop.
eloquent javascript Reversing an array
function reverse(array) {
const reversedArray = []
for(let i = array.length - 1; i >= 0; i -= 1) {
reversedArray.push(array[i])
}
return reversedArray
}
function reverseInPlace(array) {
const len = array.length - 1
const lenFor = Math.floor(len / 2)
for(let i = 0; i <= lenFor; i += 1) {
const tmp = array[i]
array[i] = array[len - i]
array[len - i] = tmp
}
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment