2

I was in codewars this morning and there is this Kata asking for a function to reverse a string passed as parameter through recursion method.

The best solution listed for this problem was this.

function reverse(str) {
    return str.length > 1 ? reverse(str.slice(1)) + str[0] : str;
}

I researched for this all this morning and I still don't know what is happening here:

+ str[0]

Can somebody please clarify this for me?

2
  • I assume "best solution" means best recursive solution? I'm doubtful this is an efficient way to reverse a string. Commented Aug 29, 2016 at 19:42
  • This is a bad solution, since the recursive call isn't in tail position. Try to convert the function to a tail recursive version to gain a better understanding of the subject. Hint: Accumulator passing style. Commented Aug 30, 2016 at 8:26

4 Answers 4

2

The essence of the function is the following:

  1. Take the substring from the second character to the last
  2. Apply the reverse function recursively
  3. Take the first character and append it to the end of the result of the recursive call
  4. Return the result

This results in the following logic, the (recursive) function calls indicated by brackets:

(A B C D E)
((B C D E) A)
(((C D E) B) A)
((((D E) C) B) A)
(((((E) D) C) B) A)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it makes sense now!
1

str.slice(1) "chops off" the first letter of the string and returns the rest of it. So 'abcd'.slice(1) gives you 'bcd'.

str[0] is the first letter of the string. 'abcd'[0] is 'a'.

So, str.slice(1) + str[0] is taking the first letter of the string and "moving" it to the end: 'abcd' becomes 'bcda'.

This does not address the recursive nature of the solution, but it answers your question about + str[0].

Comments

1

I'll try to rewrite the function in a more "human-readable" way

reverse = str => {

    // If there is still string to reverse
    if (str.length > 1) {
        let firstChar = str[0]
        let strWithoutFirstChar = str.slice(1)

        // Notice only a part of the string 'comes back' here
        // console.log(strWithoutFirstChar) // Might help
        return reverse(strWithoutFirstChar) + firstChar
    }

    // Else return result as is
    else {
        return str
    }

}

This is the same function, but without ternaries and declaring well named variables.

If you uncomment the console.log() line and call:

reverse('help');

The output should be:

elp
lp
p
'pleh'

Hope it helps!

Comments

0

The + operator is a concatenator for string. You can instead use concat this :

var reverse = str => str.length > 1 ? reverse(str.slice(1)).concat(str[0]) : str;
console.log(reverse("123456789")); // 987654321

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.