Fix accessing array length in loop condition statement.

Also, add back a missing local variable declaration.