Skip to content

Instantly share code, notes, and snippets.

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 christopherhill/ca8301d069a8fade953db27214a23e46 to your computer and use it in GitHub Desktop.
Save christopherhill/ca8301d069a8fade953db27214a23e46 to your computer and use it in GitHub Desktop.
Reverse a string with recursion
// string reverse using recursion
var str = 'abcdefg';
function reverse(original, reversed) {
if (!reversed) {
return reverse(original, [original.charAt(original.length - 1)]);
} else {
var len = reversed.length;
reversed.push(original.charAt(original.length - len - 1));
if (len < original.length - 1) {
return reverse(original, reversed);
} else {
return reversed.join('');
}
}
}
var reversed = reverse(str);
console.log(reversed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment