'Length' Property Undefined while iterating Array

后端 未结 3 895
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 04:00

Iterating an array to find the longest string. Each time I\'ve received the error Cannot read property length of undefined. Console.log tells me th

3条回答
  •  遥遥无期
    2020-12-04 04:30

    The problem is the bounds of your iteration

    for (i=0; i<=arrayL; i++) {
    

    Note that you are going to be looking for array[array.length] doing this and that will always be undefined because the 10th item in an array of 10 items is [9], [10] is undefined.

    Also, please use var here

    for (var i=0; i < arrayL; i++) {
    

提交回复
热议问题