Skip to content

Instantly share code, notes, and snippets.

@MinimumViablePerson
Created December 31, 2019 12:38
Show Gist options
  • Save MinimumViablePerson/8bd474e5a47386f86b46a09ac9539cc2 to your computer and use it in GitHub Desktop.
Save MinimumViablePerson/8bd474e5a47386f86b46a09ac9539cc2 to your computer and use it in GitHub Desktop.
Recursion from Scratch - reversing a string
const reverse = string => {
// the reverse of an empty string is: just an empty string
if (string === '') return ''
// the reverse of any other string is:
// the reverse of the rest of the string + the first character at the end
const firstCharacter = string.slice(0, 1)
const theRest = string.slice(1)
return reverse(theRest) + firstCharacter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment