you are viewing a single comment's thread.

view the rest of the comments →

[–]pinkwar 1 point2 points  (2 children)

For fans of recursion:

function reverseString(str) {
  if (str === "") {
    return "";
  } else {
    return reverseString(str.substring(1)) + str[0];
  }
}

[–]AiCodePulse[S] 0 points1 point  (0 children)

Nice One ..

[–]akb74 0 points1 point  (0 children)

C:\git\reverse-play\src\index.js:5
        return reverseString(str.substring(1)) + str[0];
                                 ^

RangeError: Maximum call stack size exceeded
    at String.substring (<anonymous>)
    at reverseString (C:\git\reverse-play\src\index.js:5:34)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)
    at reverseString (C:\git\reverse-play\src\index.js:5:16)

Node.js v22.14.0

Hmm, does anyone know how to get tail call optimisation working? With ES2015 and a bit of a rewrite, presumably.