18function.html

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 打印5次你好 
        /*
        console.log("你好");
        console.log("你好");
        console.log("你好");
        console.log("你好");
        console.log("你好");
        
        for(var i=0; i<5; i++){
            console.log("你好");
        }
        /*
          重复代码的写一次,代码的执行我们可以控制
          函数 1 提高代码复用性 2 函数封装代码执行可以被控制
          具备某种功能的代码块

          语法 
            function 函数名() {
                 // 具体代码实现
            }

          注意  函数定义后 不会自动执行,必须调用函数

          函数名();   
        */
        // 无参函数
        function printMsg() {
            // 函数体 完成特定功能的代码块
            console.log("你好");
        }
        printMsg();
        printMsg();
        printMsg();
        printMsg();
        window.printMsg(); // window.成员 简写成员

        // 函数参数
        // 求两个数较大值 然后控制台输出
        /*
           函数定义时候 小括号的参数叫形式参数(形参)
           函数调用时候 小括号的参数叫实际参数(实参) 
           函数调用时 实参->形参
        */
        function getMax(a,b) {
            console.log(arguments);
            console.log(arguments.callee);// 函数本身
            console.log(arguments.length);// 实参个数
            console.log(getMax.length);// 形参个数
            if (arguments.length != getMax.length) {
                console.log("参数个数不匹配");
            } else {
                // 10>undefined
                if (a > b) {
                    console.log(a);
                } else {
                    console.log("b");
                    console.log(b);
                }
            }
        }
        //getMax(10, 12);// 多个参数由逗号隔开
        //getMax(1, 3, 5);
        //getMax(10);//undefined
        getMax(1, 3, 5, 6, 10);

        // arguments 对象

    </script>
</body>

</html>

19function2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        table,tr,td {
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <!--
    <table>
        <tr>
            <td></td>
            <td></td>
        </tr>
        <tr>
                <td></td>
                <td></td>
            </tr>
    </table>
    -->
    <script>
        // 定义一个函数 打印m*n表格
        function printTable(m,n) {
            document.write("<table>");
            for(var i=1; i<=m; i++) {
                document.write("<tr>");
                for(var j=1; j<=n; j++) {
                    document.write("<td>aaa</td>");
                }    
                document.write("</tr>");
            }
            document.write("</table>");
        }
        /*
        printTable(3,3);
        printTable(4,3);
        */
        var m = parseInt(prompt("请输入行数"));
        var n = parseInt(prompt("请输入列数"));
        printTable(m,n);

        // 函数实现 输出1-100中,除了7的倍数和带7的数 (传参实现) 
        // 17 27 .... 77 70 71 ...  parseInt(i/10)
        function printNumber(a,b) {
            for(var i=a; i<=b; i++) {
                /*
                if(i%7===0 || i%10===7 || parseInt(i/10)===7) {
                   
                } else {
                    console.log(i);
                } 
                */
                if(i%7===0 || i%10===7 || parseInt(i/10)===7) {
                   continue; 
                }
                console.log(i);     
            }
        }
        printNumber(1,100);
    </script>
</body>
</html>

20function3.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        function getMax(a,b) {
            if(a>b) {
                return a; // 返回一个值 还可以让函数提前结束调用 有时候我们直接用return;结束函数调用
                console.log("welcome to beijing");
            } else {
                return b;
            }
        }
        var max = getMax(10,4); // max = 10
        console.log(max);
    </script>
</body>
</html>

20function4.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 1+2+3+...+100 前100项和
        function getSum(n) {
            var sum = 0;
            for(var i=1; i<=n; i++) {
                sum += i;
            }
            return sum;
        }
        // 前n项和 == 前n-1项+n
        function getSum2(n) {
            /*递归结束条件*/
            if(n==1) {
                return 1;
            }
            return getSum2(n-1)+n;
        }
        /*
           n=100 getSum2(99)+100 = getSum2(98)+99+100 = getSum(1)+2+3....+100

        */ 
        console.log(getSum2(100));
        //console.log(getSum(200));
      // 2+4+。。。+100
      function getSum3(n) {
            /*递归结束条件*/
            if(n==2) {
                return 2;
            }
            return getSum3(n-2)+n;
        }
    console.log(getSum3(100));

    /*
      斐波那契数列
      0 1 1 2 3 5 8 13 31 34
      输出第n项 第n-1项+第n-2项
    */
    function fnFB(n) {
        if(n==1) {
            return 0;
        } else if(n==2) {
            return 1
        }
        return fnFB(n-1)+fnFB(n-2);
    }
    console.log(fnFB(10));
    </script>
</body>
</html>

20function5.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
       // 函数实现 输出1-3之间能组成的奇数个数 并输出这些数
       // 组成的奇数是两位数 个位上的数!=十位上的数
       // 13 31 23 21   1(2|3) 2(1|3) 3(1|2)
       function OddCount() {
           var count = 0;
           for(var i=1; i<=3; i++) {
               for(var j=1; j<=3; j++) {
                   if(i!=j) {
                       var num = i*10+j;// ""+i+j "12"
                       if(num%2) { // "12"%2 
                           console.log(num);
                           count++;
                       }
                   }
               }
           } 
           return count;
       }
       alert(OddCount());
    </script>
</body>
</html>

 

 

 

 

posted on 2019-07-03 21:01  HiJackykun  阅读(215)  评论(0编辑  收藏  举报