Skip to content

Instantly share code, notes, and snippets.

@coreycoburn
Last active May 18, 2020 03:01
Show Gist options
  • Save coreycoburn/4fda7037e76301f1671dfe359084a8b8 to your computer and use it in GitHub Desktop.
Save coreycoburn/4fda7037e76301f1671dfe359084a8b8 to your computer and use it in GitHub Desktop.
Coding Challenges Data Structures and Algorithms
// Reverse a string
// O(n) linear time
const reversePhrase = phrase => {
phrase = phrase.split('')
const halfPhrase = Math.floor(phrase.length / 2)
const length = phrase.length - 1
for (i=0; i < halfPhrase; i++) {
let front = phrase[i]
let back = phrase[length-i]
phrase[i] = back
phrase[length-i] = front
}
return phrase.join('')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment