The - 1 should be cached rather than reapplied on each loop. Also should use !== instead of &lt;