Last active
January 27, 2020 14:18
-
-
Save ktajpuri/b5e03a86af609f3d3cb554f8dba2f847 to your computer and use it in GitHub Desktop.
Reverse a string using recursion
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
ES5 | |
function strRev(str) { | |
if(str.length === 1) { | |
return str; | |
} | |
return str.charAt(str.length-1) + strRev(str.substr(0, str.length-1)) | |
} | |
ES6 | |
const strRev = str => str.length === 1 ? str : str.charAt(str.length-1) + strRev(str.substr(0, str.length-1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment