Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created June 30, 2020 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/d07589347b3dbc560f176ff721c81c7c to your computer and use it in GitHub Desktop.
Save BetterProgramming/d07589347b3dbc560f176ff721c81c7c to your computer and use it in GitHub Desktop.
function reverseString(str) {
// base case: when there's no string to reverse
if (str === '') {
return ''
} else {
// recursive case:
// (1) grab the last character of current string,
// (2) call the same function
// (3) pass in a substring that does NOT include the last character
// (4) return (1) + (2)
return str[str.length - 1] + reverseString(str.substring(0, str.length - 1))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment