Fixing 'for' Javascript snippet