Created
December 10, 2017 12:17
-
-
Save DmitryOlkhovoi/d4aa1a4a991bd51744f6890cd21b31f1 to your computer and use it in GitHub Desktop.
eloquent javascript Reversing an array
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
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