Created
May 9, 2015 20:23
-
-
Save alexcorvi/384e3d77fe5e2634cb5d to your computer and use it in GitHub Desktop.
reversing array 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
// in a new array | |
var reverseArray = function (old_array) { | |
var new_array = []; | |
for (i=0; i<=old_array.length-1; i++) { | |
new_array[old_array.length-1 - i] = old_array[i]; | |
} | |
return new_array; | |
} | |
// in the same array | |
var reverseArrayInPlace = function (old_array) { | |
var left = ""; | |
var right = ""; | |
for (i=0; i<=Math.floor((old_array.length-1)/2); i++) { | |
left = old_array[i]; | |
right = old_array[old_array.length - 1 - i]; | |
old_array[i] = right; | |
old_array[old_array.length - 1 - i] = left; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment