JS中数组实现(倒序遍历数组,数组连接字符串)

// =================== 求最大值=====================================
    <script>
        var arr = [10,35,765,21345,678,89];
        var max = arr [0];
        for (var i=0;i< arr.length;i++) {
            if (max<arr[i]){
                max = arr [i];
            }
        }
        console.log("最大值:" + max);
    </script>
// =================== 求最小值=====================================
    <script>
        var arr = [10,35,765,21345,678,89];
        var max = arr [0];
        for (var i=0;i< arr.length;i++) {
            if (max>arr[i]){
                max = arr [i];
            }
        }
        console.log("最小值:" + max);
    </script>
    //  ============================倒序遍历数组===============================
    <script>
        var arr = [1,2,3,4,5,6,7,8,9];
        for (var i= arr.length-1;i>=0;i--){
            console.log(arr[i]);
        }
    </script>
    //===========数组拼接字符串===========第一种方法(从前面往后加)=========
    <script>
        var names = ["周杰伦","李连杰","林俊杰","张韶涵","田馥甄"];
        var str = "";
        for (var i = 0; i< names.length-1;i++){
           str += names[i] + "|"
        }
        console.log(str+ names[names.length-1]);
    </script>
    //===========数组拼接字符串===========第二种方法(从后面往前加)=========
    <script>
        var names = ["周杰伦","李连杰","林俊杰","张韶涵","田馥甄"];
        var str = "";
        for (var i = 1; i< names.length;i++){
            str +=  "|" + names[i]
        }
        console.log(names[0]+ str);
    </script>

 

// ==============去掉数组中重复的0,把其他的数据放在一个数组中============
    <script>
        var arr = [10,20,0,12,0,0,124,32];
        var newArr = [];
        for (var i=0; i<arr.length; i++){
            if (arr[i] != 0){
                newArr[newArr.length] = arr[i];
            }
        }
        console.log(newArr);
    </script>
 // =====================反转数组----------把数组中的数据位置调换=============
    <script>
        var array = [10,20,30,40,50,60];
        for (var i=0 ;i<=array.length/2;i++){
           var temp = array [i];
           array[i] = array[array.length-1-i];
            array[array.length-1-i] = temp;
        }
        console.log(array);
    </script>

//===========数列求成绩,总成绩,平均分,最大最小值===============//

<script>
    var proInput = parseInt(prompt("请输入计研17的班级人数"));
    var proScore = [];
    for (var i = 0; i< proInput; i++){
        proScore[proScore.length] = parseInt(prompt("请输入第"+ (i+1) + "个同学的成绩"));
    }
    console.log(proScore);
    var sum =0;
    var aver = 0;
    var max = proScore[0];
    var min = proScore[0];
    for (var i = 0;i< proScore.length;i++){
        sum += proScore[i];
        aver = sum/proScore.length;
        if (max<proScore[i]){
            max = proScore[i];
        }
        if (min>proScore[i]){
            min = proScore[i];
        }
    }
    console.log("班级总成绩:"+ sum);
    console.log("平均值:"+ aver);
    console.log("最大值:"+ max);
    console.log("最小值:"+ min);
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值