Reverse a string (recursion)

Easy TimeO(n²) (slice + concat copies) SpaceO(n)

Given a string s, return it reversed: "hello" becomes "olleh". No loops, and no calling reverse(). Characters keep their identity and only the order changes, so a string of length 0 or 1 is already its own reverse.

Examples

Example 1

Input
s = "hello"
Output
"olleh"
The same five characters, last to first.

Example 2

Input
s = ""
Output
""
Nothing to turn around, and the empty string comes back.

The Code

function reverseString(s) {
  if (s.length <= 1) return s;
  return reverseString(s.slice(1)) + s[0];
}
reverseString("hello");
Done
Step through reverseString("hello") call by call