Skip to content

Instantly share code, notes, and snippets.

@jonDowdle
Last active December 31, 2015 15:09
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 jonDowdle/8005434 to your computer and use it in GitHub Desktop.
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/
/*
* 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