I have 2 arrows one increments a counter by 1 and the left one decreases it the problem I am trying to solve is good logic to deal with negative number (it should go to the last item in the list) and at the other end when the counter is over the max number of items it should return to 0.
Am looking for some nice logic to achieve this.
You can use the modulo operator for this:
If you want 1..total instead of 0..total-1, use
((num + total) % total) + 1So the (pseudo-)code could look like this:
The
+totalis necessary since the behaviour of the modulo operator for negative values is not consistent across languages and that way it always works since you never get a negative value.