hi there, why the while loop don’t continue to -infinity and stop on -1

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.

The following decrementing changes len, but has no influece anymore of the condition, because of its postfix nature of the decrement operator --.

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));

Leave a Comment