Fixed iteration bug in javascript