I know there is a more elegant way to write this.  The logic is solid and doing what I want it to, but I feel like when written like this, it is awfully cumbersome to have these many if statements essentially doing the same thing.if (forward) {    if (counter == listEnd) {        counter = 0;    }    else {        counter++;    }    if (nextCounter == listEnd) {        nextCounter = 0;    }    else {        nextCounter++;    }    if (prevCounter == listEnd) {        prevCounter = 0;    }    else {        prevCounter++;    }} // end if
Comparing multiple variables and assigning new values respectively
javascript
Introduce a function:var boundedIncrement = function(current, maximum) {    return current === maximum ? 0 : current + 1;};Then use it:if (forward) {    counter = boundedIncrement(counter, listEnd);    nextCounter = boundedIncrement(nextCounter, listEnd);    prevCounter = boundedIncrement(prevCounter, listEnd);}No more logic duplication.You could also close over listEnd if you wanted to have a single argumented function:if (forward) {    var boundedIncrement = function(current) {         return current === listEnd ? 0 : current + 1;    };    counter = boundedIncrement(counter);    nextCounter = boundedIncrement(nextCounter);    prevCounter = boundedIncrement(prevCounter);}