re: Intro to Recursion in JS VIEW POST


1) You can solve any problem without Recursion, so it is often looked over for beginners.

Depends on the language, Scheme for example has no looping constructs other than recursion.

2) It’s advantages are not super obvious.

Some mathematical definitions are expressed recursively, so they lend themselves to be programmed in the same way. Other than that it really depends on whether or not your language supports proper tail calls (in which case the stack will not grow at all).

3) It can be flat out confusing.

IMHO it's one of the concepts that people make out harder to be than it actually is. Non-infinite loops also have base cases and need to get closer to them, so there's really not much of a difference there. Look at the most simple for loop in JS for example:

for (n = 10; n > 0; n--) { 

n > 0 is our base case, n-- gets us closer to that on every iteration.

Code of Conduct Report abuse