since locations.length doesn't change, use a variable to store it, otherwise locations.length is evaluated on every iteration.