Skip to content

Instantly share code, notes, and snippets.

@jialinhuang00
Last active October 2, 2017 05:54
Show Gist options
  • Select an option

  • Save jialinhuang00/29a273874a639f62e484209abfe2b19f to your computer and use it in GitHub Desktop.

Select an option

Save jialinhuang00/29a273874a639f62e484209abfe2b19f to your computer and use it in GitHub Desktop.
/*-----------------------------------------------------------------------------
1.swap the elements
2.temporary variable is useful.
the reference is from UdemyCourse: LearningAlgorithmsInJavascriptFromScratch
-----------------------------------------------------------------------------*/
function reverseArrayInPlace(arr) {
// dsakhpr d <-> r s <-> p a <-> h
for (var i = 0; i < arr.length / 2 ; i++) {
// swapping
var tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
return arr;
}
reverseArrayInPlace([1,'421',[41]])
// [ [ 41 ], '421', 1 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment