Issue with wrap around

I want to wrap around I try

        const lastIndex = showroom.cars.length - 1;

        if(index < 0){
            index = lastIndex;
            
        }
        if(index > lastIndex){
            index = 0;
        }
        showroom.showOne(index);

but the index is decreasing to -1 or increasing above 4 so 5
the car changes from first to last and from last to first
for example when I change from first, so go left
the car is last but the index is -1 :expressionless:

ok % ?
index = x % cars.length;

ok sorry this works I didn’t notice it, it wraps around
but how to make it decrementing when reach from 0 to 4 and then 3?
this not working
but wraps around it changes from 0 to 4
and from 4 to 0

maybe try this:

if(index < 0){
index = lastIndex;

    }
    else if(index > lastIndex){
        index = 0;
    }
1 Like