Created
January 20, 2020 18:28
-
-
Save Giagnus64/bf4a461342bcf147bdbba72c9bb633b2 to your computer and use it in GitHub Desktop.
Reverse a string
This file contains hidden or 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 reverse(str){ | |
| // if we arrive at the final character of the string, return that character | |
| if (str.length === 1) return str[0]; | |
| //otherwise return the last character of the string, plus the reverse function called on a sliced version of that string | |
| return str[str.length - 1] + reverse(str.slice(0, str.length - 1)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment