I have a doubt about using while i--
and while -ii
in code.
ar points = [30,100];
document.getElementById("demo").innerHTML = myArrayMax(points);
function myArrayMax(arr) {
var len = arr.length;
var max = -Infinity;
while (len--) {
if (arr[len] > max) {
max = arr[len];
}
}
return max;
}
In the above code, since len
is 2
, I am guessing the len
value becomes 1 in the initial iteration involving the if statement. After this iteration, in the while (len--)
, does len become 0 or 1? Judging from this, it seems the arr[2]
never gets evaluated.
And what difference will the code make if while --len
was used instead? Thank you.