The first example works perfectly fine for me so I think the error is happening somewhere else in your code or template.
The second example is using the variable i for the outside and inside loops so that's an issue. FYI, its common to use i --> j --> k for loops.
Sounds like a refresh issue.
Try to add
app.scriptPreferences.enableRedraw = false;
before you for(…) loop, then restore
app.scriptPreferences.enableRedraw = true;
at the end of the process.
Thank you very much, Marc. This is exactly right. Works perfectly!
Thanks you for replying, BSKTCreation. But I don't think that is the reason. Both tries worked perfectly in the ExtendScript, it was just when I exported it that it started messing up. And I dont think it has to do with the i variable because I redefined it just before the loop, shouldn't that stop any problems with i variables in other scopes? Marc's answer seems to have worked, though. Thank you, though.
You're right, I didn't give the 2nd script a close enough look and missed that (read it too quickly i guess). Glad Marc got you going though.