For more information about recursion, see Chapter 1st. The following is a simple recursive programming question. Question: What is the sum that has been accumulated from 1 to 100?
Non-recursive statement:
The Code is as follows:
1run: function (){
2 var sum = 0;
3 for (var I = 1; I <= 100; I ++ ){
4 sum = sum + I;
5}
6 console. log (sum );
7}
Recursive Syntax:
The Code is as follows:
Var testCase = {
Sum: 0,
Run: function (n ){
If (n> = 100 ){
Return 100;
}
Else {
Sum = n + testCase. run (n + 1 );
Return sum;
}
}
};
Console. log (testCase. run (1 ));
The above code is a lot of online searches, and the following code is equivalent to it:
The Code is as follows:
Console. log (function (n ){
Var sum = 0;
If (n <= 1 ){
Return 1;
}
Else {
Sum = arguments. callee (n-1) + n;
Return sum;
}
}) (100 ));
This method is easy to learn. The above is linear recursion. It's okay to get started with recursion, and the algorithm's performance efficiency will be worse.