Would you be so kind and explain how "else if" statement in the below piece of code returns the second highest value from the array?
Especially that there is no AND operator (&&) within "else if statement". I mean for ex.:
else if (max2 < array[i] && max1 > array[i])
I found people using this kind of approach on stack but they did not give any detailed explanations ("why it works"):
var arr1 = [2, 3, 1, 6, 100, 49, 5, 7, 8, 9];
function getSecondMaxNumber(array) {
var max1 = 0;
var max2 = 0;
for (var i = 0; i <= array.length; i++) {
if (max1 < array[i]) {
max1 = array[i];
} else if (max2 < array[i]) {
max2 = array[i];
}
}
return max2;
}
console.log(getSecondMaxNumber(arr1));
Aucun commentaire:
Enregistrer un commentaire