Not enough information to answer the question; it is not always possible to say whether the user has moved "right" or "left" only with current and previous indices.
For example, say current = 0 and previous = 1 .
Has the user moved left or has the user moved right and overflowed to 0 because there are only two pages? Impossible to say.
If you do not know the number of pages, here are some rough solutions:
// Doesn't handle wrap arounds properly. bool movedRight = current == previous + 1; // Assumes that being at the start point always means an overflow occurred. bool movedRight = (current == previous + 1) || current == 0; // Assumes that being at the start point always means an overflow occurred, // except when we were previously at the second element. bool movedRight = (current == previous + 1) || (current == 0 && previous != 1);
If you know the number of pages, perhaps more intuitively:
but thereโs still not enough information to tell what the user did when there is only one or two pages: in these cases, moving left and right gives equivalent โresultsโ. If you must distinguish between left and right even in these situations, the obvious solution would be to simply keep the direction in which the user is called separately. You should probably not try to figure out what cannot be calculated. :)
source share