working out javascript logic