DEV Community

[Comment from a deleted post]
Collapse
 
aghost7 profile image
Jonathan Boudreau

The fun way:


function reverseFun(str) {
    const rest = str.length > 1 ? reverseFun(str.substring(1)) : ''
    return rest + str[0] || ''
}
Collapse
 
ycmjason profile image
YCM Jason • Edited

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];
};
Collapse
 
caseycole589 profile image
Casey Cole

const reverseString = cs => cs.length <=0
? ''
: reverseString(cs.substring(1)) + cs[0];