Skip to content

Instantly share code, notes, and snippets.

@AliceWonderland
Forked from anonymous/8.3 Reverse Array.js
Created April 18, 2017 07:28
Show Gist options
  • Save AliceWonderland/c59262574347d20b3375463edca924ed to your computer and use it in GitHub Desktop.
Save AliceWonderland/c59262574347d20b3375463edca924ed to your computer and use it in GitHub Desktop.
8.3 Reverse Array created by smillaraaq - https://repl.it/HJlK/4
var myArray = [1, 2, 3, 4];
reverse(myArray);
console.log(myArray) // [4, 3, 2, 1]
function reverse(arr){
var cloneArray=arr.slice();
var tmpArray=[];
for(var i=0;i<arr.length;i++){
tmpArray.push(cloneArray.pop());
}
myArray=tmpArray.slice();
}
/* SCHOOL SOLUTION
function reverse(arr) {
var midPoint = Math.floor(arr.length/2);
console.log(midPoint)
for (var i=0; i<midPoint; i++) {
var temp = arr[i];
arr[i] = arr[(arr.length-1)-i];
arr[(arr.length-1)-i] = temp;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment