zoukankan      html  css  js  c++  java
  • JS基础:求一组数中的最大最小值,以及所在位置

     1         var arr = [0, 5, -3, 6, 2, -6, 10];
     2         //定义一个最大值和一个最小值,把他们的索引值赋值给固定的两个变量
     3         var maxValue = arr[0];
     4         var minValue = arr[0];
     5         var maxIndex = 0;
     6         var minIndex = 0;
     7         for (var i = 1; i < arr.length; i++) {
     8             if(arr[i] > maxValue){
     9                 //把这个元素赋值给最大值,把他对应的索引值,赋值给maxIndex
    10                 maxValue = arr[i];
    11                 maxIndex = i;
    12             }
    13             //如果数组中的元素小于我们定义的最小值
    14             if (arr[i] < minValue) {
    15                 minValue = arr[i];
    16                 minIndex = i;
    17             }
    18         }
    19         console.log(maxValue);
    20         console.log(maxIndex);
    21         console.log(minValue);
    22         console.log(minIndex);

     

    你必须穷尽一生磨练技能,这就是成功的秘诀,也是让人家敬重的关键。
  • 相关阅读:
    数据结构 1
    MyBatis 7
    MyBatis 6
    MyBatis 5
    MaBatis 4
    MyBatis 3
    目录和文件管理
    Linux常用命令精讲
    Sentos7.4安装说明
    RIP
  • 原文地址:https://www.cnblogs.com/knuzy/p/8809534.html
Copyright © 2011-2022 走看看