Recursive
The essence of recursion is to use the function itself to solve the problem of the idea.
definition of recursion (excerpt):
The programming technique called by the program itself is called recursion (recursion). Recursion as an algorithm is widely used in programming language . A procedure or function has a method of directly or indirectly invoking itself in its definition or description, which usually transforms a large and complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program. The ability to recursion is to define an infinite set of objects with limited statements . In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
the conditions that make up a recursive requirement:
1. The sub-problem should be the same as the original problem, and simpler.
2. It is not possible to call itself indefinitely, and there must be an exit, simplifying the process of non-recursive status.
first, use the usual way to solve the problem:
There are a bunch of peaches, monkeys eat half a day to throw one, the sixth day of the remaining 1, ask how many Peaches started
var t=1; for (i=0;i<6;i++) { t= (t+1) * *; } Alert (t);
If we use a For loop to solve the problem, we need to define a loop that loops six times, that is, six days, the loop (the number of the previous day = the number plus one multiplied by two), and the result is obtained.
If you use recursion to find the number of the third day:
// recursive // number of day = (number of next day + 1) * * // function Explicit: Give a number of days to return the remainder of the day function Shuliang (ts) { if(ts==6) { return 1; } return (Shuliang (ts+1) +1) * *; } Alert (Shuliang (3));
Here we first want to clear: the number of the day = (Next day quantity + 1)
Then clear the function: give a number of days to return the remainder of the day
function Ideas:
If the number of days is 6, return 1.
Return: Number of day = (number of next day + 1) * *
the procedure for running a function is:
//days 3, starting the first calculation functionShuliang (3){ if(ts==6){ return1; }//days are not equal to 6, skip return(Shuliang (3+1) +1) * *;//call itself, days =3+1, continue function } //Number of Days is 4, second calculation functionShuliang (4){ if(ts==6){ return1; }//days are not equal to 6, skip return(Shuliang (4+1) +1) * *;//call itself, days =4+1, continue function } //Number of Days is 5, third calculation functionShuliang (5){ if(ts==6){ return1; }//days are not equal to 6, skip return(Shuliang (5+1) +1) * *;//call itself, days =5+1, continue function } //Number of days is 6, fourth time calculation functionShuliang (3){ if(ts==6){ return1; }//days equals 6, returns 1 } //The fourth time calculation returns 1 to the third calculation functionShuliang (5){ return(+) * *;//number of days equals 1, return (*2=4) } //The third calculation returns 4 to the second calculation functionShuliang (4){ return(4+1) * *;//days equals 4, return (4+1) *2=10 } //The second calculation returns 10 to the first calculation functionShuliang (3){ return(10+1) * *;//days equals 10, return (10+1) *2=22 } //Finally, the number returned on the third day is
When the function executes, ts=3, and then enters the function
First time calculation:
TS is not equal to 6, over
Call function itself, ts=4, continue function
Second calculation:
TS is not equal to 6, over
Call function itself, ts=5, continue function
Third-time calculation:
TS is not equal to 6, over
Call function itself, ts=6, continue function
Fourth time calculation:
TS equals 6, returns 1
The fourth time calculation returns 1 to the third calculation,
The third calculation returns (*2=4) to the second calculation,
The second calculation returns (4+1) *2=10 to the first calculation
First calculated return (10+1) *2=22
The number of the third day is 22.
use recursive thinking to find the number of files under a folder
function function: Give the path to a folder, return to the number of files under the folder
function Shuliang (folder path) { var sum=0; if (is file) { sum+ +; } Else { sum=sum+Shuliang (folder path); } }
Open Folder
If you encounter a file, the number +1
If you encounter a folder, execute a function, open a folder, if you encounter a file, number +1, if you encounter a folder, execute the function ...
Recursion of JavaScript functions