Skip to content

Instantly share code, notes, and snippets.

@TimBlock
Created January 14, 2016 15:54
Show Gist options
  • Select an option

  • Save TimBlock/25e2856cb9d4622d0748 to your computer and use it in GitHub Desktop.

Select an option

Save TimBlock/25e2856cb9d4622d0748 to your computer and use it in GitHub Desktop.
var reverseArray = function(arr) {
var arr2 = [];
for (var i = arr.length - 1; i >= 0; i--)
arr2.push(arr[i]);
return arr2;
};
var reverseArrayInPlace = function(arr) {
var arr2 = 0;
for (var i = 0; i < arr.length / 2; i++) {
arr2 = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = arr2;
}
};
console.log(reverseArray(["A", "B", "C"]));
// → ["C", "B", "A"];
var arrayValue = [1, 2, 3, 4, 5];
reverseArrayInPlace(arrayValue);
console.log(arrayValue);
// → [5, 4, 3, 2, 1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment