码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-28 15:52:32      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:war   rom   均值   sum   inpu   拼接   成绩   mes   倒序   

// =================== 求最大值=====================================
<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>

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

标签:war   rom   均值   sum   inpu   拼接   成绩   mes   倒序   

原文地址:https://www.cnblogs.com/ZXH-null/p/12111937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!