Infinitely Cycle Through an Array

This is a companion discussion topic for the original entry at https://www.kirupa.com/javascript/infinitely_cycle_through_array.htm
2 Likes

What about both directions [ < Infinitely > ]. Sometimes going to the right around the world is much longer than stepping left directly to your destination. :wink:

1 Like

Thought more about this, and I don’t know of a clever solution outside of the developer using some heuristic to determine direction. For example, if you know your array is sorted, you could use the size of the item you are looking for [along with the value of the first and last item] as a signal in which direction to cycle by.

This response confused me for a while until I realized you were talking about how to know when to go left or right and not simply providing the option of going left or right :upside_down_face:

2 Likes

Haha! Yes. After a day full of Slack convos, e-mails, and talking, my brain is a :fried_egg: at the point I wrote my earlier response :stuck_out_tongue:

Now I am hungry.

:hatching_chick: