1. 主页
  2. 文档
  3. JS
  4. 函数

函数

        alert();
        console.log();
        var a = 1;
        var b = 'dasfasdfads';

        switch(a){
            case 1:
            case 2:
                alert('2222');
                break;
        }

        var age = 23;

        var ae = age > 30 ? '成年人' : '未成年人';
        alert(ae);
        console.log(ae);

        if(a > 10){
            alert(1);
        }else{
            alert(2);
        }

        for(var i =1; i<=10; i++){
            alert(i);
        }

        break;
        continue;

        var ar1 = [1,2,3,4,5];
        var ar2 = new Array('a','b','c','d');
        var ar3 = new Array(8);

        var ar1 = [1,2,3,4,5];
        var ar2 = new Array('a','b','c','d');
        var ar3 = new Array(8);
        console.log(ar1);

        ar1.pop(4);
        console.log(ar1);

        ar1.push(0);
        console.log(ar1);

        ar1.unshift(0);
        console.log(ar1);


        ar1.push();
        ar1.pop();
        ar1.unshift();
        ar1.shift();

        ar1.join();
        console.log(ar1);


        //=======================================================
        var ar1 = [1,2,3,4,5];
        var ar2 = new Array('a','b','c','d');
        var ar3 = new Array(8);
        var ar4 = [1,2,3,4,5,5,7,3,2,1];

        //ar1.join();
        //console.log(ar1);

        // 求数组的总和
        var num = 0;
        for(var i = 0; i<ar1.length; i++){
            num+=ar1[i]; //num = num + 1  +2  +3 +4 +5
        }
        console.log(num);

        ar1.splice(1,2,0,0);
        console.log(ar1);

        //求和
        var num1 = 0;
        for(var i = 0; i<ar1.length; i++){
            num1+=ar1[i];
        }
        console.log(num1);

        //获得最大的值。获得最小的值。
        var max = 0;
        var min = 0;

        for(var i = 0; i<ar1.length; i++){
            if(ar1[i] > min){
                max = ar1[i]; 
            }else if(ar1[i] < min){
                min = ar1[i];
            }
        }
        console.log(max);
        console.log(min);

        // 数组去重复。
        var narr4 = [];// 1 
        for(var i=0; i<ar4.length; i++){
            if(!narr4.includes(ar4[i])){
                narr4.push(ar4[i]);
            }
        }
        console.log(narr4);


        //4-5 冒泡排序
        var ar = [1,33,44,5,6,8,9,0]; //0,1 

        for(var i=1; i<ar.length; i++){
            for(var j=ar.length-1; j>=i; j--){
                if(ar[j] < ar[j-1]){
                    var temp = ar[j];
                    ar[j] = ar[j-1];
                    ar[j-1] = temp;
            }
        }
这篇文章对您有用吗?

我们要如何帮助您?