Created
March 23, 2019 15:31
-
-
Save marsgpl/9649012ed4d4610f3f3b6a0f1d82fe34 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
| const rotateWords = function(arr) { | |
| rotateWords.reverse(arr, 0, arr.length-1) | |
| let firstSpaceIndex = arr.indexOf(" ") | |
| let secondSpaceIndex = arr.indexOf(" ", firstSpaceIndex + 1) | |
| rotateWords.reverse(arr, 0, firstSpaceIndex - 1) | |
| rotateWords.reverse(arr, firstSpaceIndex + 1, secondSpaceIndex - 1) | |
| rotateWords.reverse(arr, secondSpaceIndex + 1, arr.length - 1) | |
| return arr | |
| } | |
| rotateWords.reverse = function(arr, from, to) { | |
| while ( from < to ) { | |
| [ arr[to], arr[from] ] = [ arr[from], arr[to] ] | |
| from++ | |
| to-- | |
| } | |
| } | |
| console.log(rotateWords("perfect makes practice".split("")).join("")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment