JS递归原理

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function fun(num) {
            console.log(num)
            if (num == 0 || num == 1) {
                return 1;
            }

            return fun(num - 2) + fun(num - 1)
        }
        fun(5);
        // 输出结果为
        // 5
        // 3
        // 1
        // 2
        // 0
        // 1
        // 4
        // 2
        // 0
        // 1
        // 3
        // 1
        // 2
        // 0
        // 1
    </script>
</body>

</html>

原文地址:https://www.cnblogs.com/tengx/p/12366633.html