Home > Web Front-end > JS Tutorial > Summary of basic JavaScript knowledge (7) Recursion

Summary of basic JavaScript knowledge (7) Recursion

php中世界最好的语言
Release: 2018-03-10 13:27:59
Original
1389 people have browsed it

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) + ..
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template