It seems like the issue may be arising because either n[i]
is not defined or longest
is also undefined, and n[i]
is never of type string.
In the first block of code, you are using logical AND to test both conditions. If my assumptions are correct, then the left-hand side of the &&
operator evaluates to true. Since there is true && ...
, the following code must be checked to determine the result. This means that n[i].length > longest.length
is being evaluated, which triggers the error.
In the second block of code, the second condition is never reached. This is because, as I mentioned earlier, the first check typeof n[i] !== 'string'
returns true, causing the continue;
statement to run every time. As a result, the second expression is never evaluated, effectively hiding the error from view but still present.
Update: A perceptive observation by @OmarElawady revealed that you are actually assigning an integer to n
, making it impossible to index as an array. Consequently, n[i]
will certainly be undefined. Additionally, there is a possibility that longest
is undefined too, although this cannot be confirmed based on the provided code. Kudos to @OmarElawady for pointing this out!