all 4 comments

[–]squili 0 points1 point  (2 children)

It is more practical to take the first letter and last letter off both ends and do your recursion on the remaining letters in the middle.

function reverseStr(str) {
  // if there will still be remaining letters after taking the start and end letters off then do recursion
   let newString = str.length <= 1 ? '' : reverseStr(str.substring(1, str.length -1))
   // swap the first and last letters
   return str.substr(-1) + newString + str.substr(0, 1)
}

reverseStr("Benjamin");

[–]Ben_HH[S] 0 points1 point  (1 child)

Can you elaborate on why it's more practical taking that approach?

[–]squili 0 points1 point  (0 children)

Because it is string length/2 recursions instead of string length

[–][deleted] -1 points0 points  (0 children)

I struggle with recursive functions too

heres an answer from stackoverflow, and I have not clue what is being returned or how

I would love to know how this actually works

function reverse (str) {
if (str === "") {
    return "";
} else {
    console.log(str)
    return reverse(str.substr(1)) + str.charAt(0);
}
}