DEV Community

Discussion on: This simple math hack lets you create an image carousel without any if statements

Collapse
 
ranewallin profile image
Rane Wallin

I don't think this will work as written. You only add the number of elements to the first part if you are going backwards. This looks like it's adding it either way. If I am at the last element (2) in this example then this would return in the next index being 3 instead of 0.

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Wrong. Adding either way works. 3 modulo 3 is zero, as is 6 modulo 3, 9 modulo 3 etc.