accessing array length in loop, and missing local variable declaration

Fix accessing array length in loop condition statement.

Also, add back a possible missing local variable declaration.