Sunday, 15 September 2013

Largest item in an array

Largest item in an array

I have the following code from another answer here on Stackoverflow:
var myarray = ['a','abcde','ab'];
var maxlen = 0;
for (i=0; i<myarray.length; i++) {
if (myarray[i].length>maxlen) {
maxlen = myarray[i].length;
}
}
I understand everything about this, apart from why creating a maxlen = 0
and then using the if operator selects the largest length.
Surely because ab is also larger than 0, that could also become the
variable maxlen? Why was abcde chosen instead?

No comments:

Post a Comment