Skip to content

Instantly share code, notes, and snippets.

@marsgpl
Created March 23, 2019 15:31
Show Gist options
  • Select an option

  • Save marsgpl/9649012ed4d4610f3f3b6a0f1d82fe34 to your computer and use it in GitHub Desktop.

Select an option

Save marsgpl/9649012ed4d4610f3f3b6a0f1d82fe34 to your computer and use it in GitHub Desktop.
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