This time I will bring you a basic knowledge summary of JavaScript. There are a total of eleven knowledge points. Basic JavaScript knowledge summary (7)RecursionThe following is a practical case, one Get up and take a look.
Write a functionRealize the factorial of n
n! = n*(n-1)!; function mul (n){ //n的阶乘 //for(var i = 1; i <= n;i ++){ // num *= i; //} if(n == 1){ return 1; } return n*mul(n-1);}// 递归mul(5);//找规律//找出口//唯一好处代码简洁 mul(5) ==> 5*mul(4); mul(4) ==> 4*mul(3); mul(3) ==> 3*mul(2); mul(2) ==> 2*mul(1); //找规律//找出口//例子:写一个斐波那契数列//fb(n) ==fb(n-1)+fb(n-2)function fb(n){ if( n == 1 ||n ==2 ){ return 1; } return fb(n-1) + fb(n-2);} fb(5) ==> fb(4) + fb(3) fb(4) ==> fb(3) + fb(2) fb(3) ==> fb(2) + ..
I believe you have mastered the method after reading the case in this article. For more exciting things, please pay attention to the php Chinese websiteOthersrelated articles!
Related reading:
Basic JavaScript knowledge summary (6) Functions, initial scope (Part 1)
Basics Summary of JavaScript knowledge (6) Function, initial scope (Part 2)
The above is the detailed content of Summary of basic JavaScript knowledge (7) Recursion. For more information, please follow other related articles on the PHP Chinese website!