关于数组的小案例

记录实验,方便学习


    let a = [4,9,5,27];
    let max = a[0];
    for(let i = 0;i<a.length;i++){
        if(a[i]>max){
            max = a[i]
        }
    }
    console.log(max);
        /* 取大于十的数 */
    let a = [4,9,5,27,99,52];
    // let max = a[0];
    let am = [];

    let k = 0;
    for(let i = 0;i<a.length;i++){
        if(a[i] >= 10){
            // max = a[i]
            // am[k] = a[i]    //第一种方式
            // k++            //  第一种方式
            am[am.length] = a[i];   //第二种方式
        }
    }
    console.log(am);

    /* 倒序排列 */
    let a = [1,2,3,4,5];
    let b = [];

    for(let i = a.length - 1;i>=0;i--){

        b[b.length] = a[i];
    }
        console.log(b);


    /* fibonacci 方式 */
    let = fibonacci = {
        [Symbol.iterator](){
            let per = 0,cur = 1
            return{
                next(){
                    [ pre,cur ] = [ cur,pre + cur ]
                    return {done:false,value:cur}
                }
            }
        }
    }
    for (let n of fibonacci){
        if(n>1000)
            break
            console.log(n);
    }

    function fibonacci(){
        if(n<0)throw new Error("输入的数字不能小于0");
        if(n == 1 || n == 2){
            return 1;
        }else {
            return fibonacci (n - 1) + fibonacci(n - 2)
            
        }
        
    }
    console.log();

    /* 冒泡排序 */

    let a = [1,2,3,4,5];
    // let a = [5,4,3,2,1];
    // let b = [];

    for(let i = 0;i<a.length - 1;i++){
        
        for(let j = 0; j<a.length - i - 1;j++){

            if (a[j] > a[j+1]) {
                let am = a[j];
                a[j] = a[j+1];
                a[j+1] = am;
            }
        }
    }

    // console.log(a);

    /* 二维数组  */

    let a = [1,"鲁班",9,5,'hello',[546,74]];

    console.log(a[5][0]);

        /* for..of 数组*/
    let a = [1,'小七',8,'hello'];
    // console.log(a);

    for(let value of a){
        console.log(value);
    }

        function fun(num){

            console.log(num);

            var num = 123;

            console.log(num);
        }
        var num = 456;
        fun(num);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值