Skip to content

Instantly share code, notes, and snippets.

Created April 18, 2017 07:28
Show Gist options
  • Save anonymous/09996d96ced944e4414847bfb459d64d to your computer and use it in GitHub Desktop.
Save anonymous/09996d96ced944e4414847bfb459d64d 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