Skip to content

Instantly share code, notes, and snippets.

@kosperera
Created August 5, 2014 19:48
Show Gist options
  • Save kosperera/d1e9a6c041c4d6d26de2 to your computer and use it in GitHub Desktop.
Save kosperera/d1e9a6c041c4d6d26de2 to your computer and use it in GitHub Desktop.
Reverse an array without using another array. This method is called Temporary Swap and it only runs for half of the array.
function reverse(arr)
{
var left = null;
var right = null;
var length = arr.length;
for (left = 0; left < length / 2; left += 1)
{
right = length - 1 - left;
var temporary = arr[left];
arr[left] = arr[right];
arr[right] = temporary;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment