Last active
December 31, 2015 15:09
-
-
Save jonDowdle/8005434 to your computer and use it in GitHub Desktop.
A quick morning brain warmup. Instead of using the array method reverse, I decided to refresh my recursion skills a bit. I'm glad I did. I find this solution quite a bit more elegant (to the eyes that is). http://www.codewars.com/dojo/katas/5168bb5dfe9a00b126000018/
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
/* | |
* Reverse whatever string is passed into solution. | |
* solution('world'); // returns 'dlrow' | |
* | |
*/ | |
function solution( str ) { | |
// Our answer | |
var answer = ""; | |
// Stop recursion when we have no more string | |
if( str.length == 0 ) { | |
return answer; | |
} | |
// Take the last letter and recurse with the rest of the string | |
answer = str.slice( -1 ) + solution( str.slice( 0, -1 ) ); | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment