Last active
October 2, 2017 05:54
-
-
Save jialinhuang00/29a273874a639f62e484209abfe2b19f to your computer and use it in GitHub Desktop.
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
| /*----------------------------------------------------------------------------- | |
| 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