Created
June 30, 2020 14:49
-
-
Save BetterProgramming/d07589347b3dbc560f176ff721c81c7c to your computer and use it in GitHub Desktop.
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
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