Created
December 31, 2017 14:24
-
-
Save garybunofsky/7597b8648bd3b60b1af2e1100ddebcc9 to your computer and use it in GitHub Desktop.
Eloquent Javascript: 04 Data Structures, Reverse an Array // source https://jsbin.com/qejuqik
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Eloquent Javascript: 04 Data Structures, Reverse an Array</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
var reverseArray = function(myArray) { | |
var result = []; | |
var length = myArray.length; | |
for (var i = 0; i < length; i++) { | |
result.unshift(myArray[i]); | |
} | |
return result; | |
} | |
var reverseArrayInPlace = function(array) { | |
for (var i = 0; i < (array.length / 2); i++) { | |
var old = array[i]; | |
array[i] = array[array.length - 1 - i]; | |
array[array.length - 1 - i] = old; | |
} | |
return array; | |
} | |
console.log(reverseArrayInPlace([1,2,3,4,5])); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">var reverseArray = function(myArray) { | |
var result = []; | |
var length = myArray.length; | |
for (var i = 0; i < length; i++) { | |
result.unshift(myArray[i]); | |
} | |
return result; | |
} | |
var reverseArrayInPlace = function(array) { | |
for (var i = 0; i < (array.length / 2); i++) { | |
var old = array[i]; | |
array[i] = array[array.length - 1 - i]; | |
array[array.length - 1 - i] = old; | |
} | |
return array; | |
} | |
console.log(reverseArrayInPlace([1,2,3,4,5]));</script></body> | |
</html> |
This file contains 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 reverseArray = function(myArray) { | |
var result = []; | |
var length = myArray.length; | |
for (var i = 0; i < length; i++) { | |
result.unshift(myArray[i]); | |
} | |
return result; | |
} | |
var reverseArrayInPlace = function(array) { | |
for (var i = 0; i < (array.length / 2); i++) { | |
var old = array[i]; | |
array[i] = array[array.length - 1 - i]; | |
array[array.length - 1 - i] = old; | |
} | |
return array; | |
} | |
console.log(reverseArrayInPlace([1,2,3,4,5])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment