Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  121] [ 4]  / answers: 1 / hits: 21827  / 14 Years ago, tue, february 1, 2011, 12:00:00

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?


More From » recursion

 Answers
61

Something like:



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


So the function is recursive as it calls itself to do the work.


[#93961] Sunday, January 30, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brennanm

Total Points: 510
Total Questions: 103
Total Answers: 95

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
brennanm questions
Thu, Jan 9, 20, 00:00, 5 Years ago
Thu, Sep 26, 19, 00:00, 5 Years ago
Thu, Aug 29, 19, 00:00, 5 Years ago
;