Javascript image array loop, I have tried using a 'for' loop but it failed