Recursion is the one idea I constantly use while I solve coding problems. Most of the time I don’t start by thinking “RECURSION WILL SOLVE THIS!”. However recursion just ends up being the logical way to reach an answer. In my professional opinion recursion is the purest form of coding; write a function that will call itself until you get what you want! To implement recursion we will create a helper algorithm. 1) Identify what the smallest input is. 2) Continually break down a larger input into smaller inputs and pass those smaller inputs back into itself until you get the desired answer. 3) Define a “base case” that will stop the Recursion should the answer not be found.

Let’s look at the idea of Recursion first. We are writing code that will execute itself until we get a desired answer or reach a base case. Essentially we are creating a loop. I will illustrate this with pseudo code:

for (let recursion =()=>{ …answer? answer = true : false} ; answer === false; recursion())

Much like a traditional for loop the above pseudo code will continue while the second condition is true; the recursion will continue until answer === true. At this point the second statement of the for loop is false terminating the loop. Above if answer === false recursion will call itself again. This is the general idea of recursion. This is why creating a base case is essential to prevent an infinite loop. The “answer” we are looking for might not be present causing recursion to run until the sun burns out.

#algorithms #javascript #recursion #tutorial-for-beginners #iteration #recursion-explained #what-is-recursion #programming

 While You Don't Understand Recursion, Read Recursion: by Randy Taylor
1.15 GEEK