Skip to content

Instantly share code, notes, and snippets.

@garybunofsky
Created December 31, 2017 14:24
Show Gist options
  • Save garybunofsky/7597b8648bd3b60b1af2e1100ddebcc9 to your computer and use it in GitHub Desktop.
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
<!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>
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