Skip to content

Instantly share code, notes, and snippets.

@alexcorvi
Created May 9, 2015 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcorvi/384e3d77fe5e2634cb5d to your computer and use it in GitHub Desktop.
Save alexcorvi/384e3d77fe5e2634cb5d to your computer and use it in GitHub Desktop.
reversing array elements
// 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