Created
December 31, 2019 12:38
-
-
Save MinimumViablePerson/8bd474e5a47386f86b46a09ac9539cc2 to your computer and use it in GitHub Desktop.
Recursion from Scratch - reversing a string
This file contains 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 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