-
-
Save AliceWonderland/c59262574347d20b3375463edca924ed to your computer and use it in GitHub Desktop.
8.3 Reverse Array created by smillaraaq - https://repl.it/HJlK/4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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