The fun way:
function reverseFun(str) { const rest = str.length > 1 ? reverseFun(str.substring(1)) : '' return rest + str[0] || '' }
I prefer writing the base case in a guard clause. I think it is more elegant.
const reverseString = cs => { if (cs.length <= 0) return ''; return reverseString(cs.substring(1)) + cs[0]; };
const reverseString = cs => cs.length <=0 ? '' : reverseString(cs.substring(1)) + cs[0];
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
The fun way:
I prefer writing the base case in a guard clause. I think it is more elegant.
const reverseString = cs => cs.length <=0
? ''
: reverseString(cs.substring(1)) + cs[0];