I'm a pretty experienced frontend engineer with a weak CS background. I'm trying to get my head around the concept of recursion. Most of the examples and purported explanations I can find just aren't explaining it in a way I find easy to understand.
I set myself a task of writing a function that will reverse a string recursively. I know there has to be a base condition (i.e. the solution is found), but I can't figure out how to actually write something like this and could use a demo to study.
Could someone provide a sample function?
A 25% faster function: jsperf.com
It is verbose, but I like making it easy to understand in logical steps:
}
Then call it like:
So far the best I think:
Try this:
One line of code using ternary operators you can easily reverse it.
Explanation: if string exists (if not null) then return recursion otherwise stop the recursion.
Function call:
This is a pretty straightforward C# implementation of the algorithm you asked for. I think it could be rewritten in javascript pretty easily.