简体   繁体   English

嗨,为什么 while 循环不继续到 -infinity 并在 -1 上停止

[英]hi there, why the while loop don't continue to -infinity and stop on -1

Why don't continue the while loop until -infinity?为什么不继续while循环直到-infinity?

 <script>
        var number=[100,3,250,99,70,1,70];
        document.getElementById("demo1").innerHTML=maxValue(number);
        function maxValue(arr){
        var len=arr.length;
        var min=Infinity;
        while(len--)
        {
          if(arr[len]<min){
            min=arr[len];
          }
        }
          return min;
        }
      </script>

It uses the condition of the while statement with the value of len and if it is zero, then the iteration stops, because this value is falsy .它使用while语句的条件和len的值,如果它为零,则迭代停止,因为该值是假的

The following decrementing changes len , but has no influece anymore of the condition, because of its postfix nature of the decrement operator -- .以下递减更改len ,但不再影响条件,因为递减运算符--后缀性质。

 function maxValue(arr) { var len = arr.length; var min = Infinity; while (len--) { console.log('len', len); if (arr[len] < min) { min = arr[len]; } } return min; } var number = [100, 3, 250, 99, 70, 1, 70]; console.log(maxValue(number));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM