JavaScript does not do block scoping, only function scoping, so declaring `var i` here, and then again later, means you're redeclaring it. `for(var i=...` and `var i; for(i=...` are equivalent in javascript -- lift this var out, explicitly.