There is HTML
<div class="why"> <div class="why__slide why__slide-active"></div> <div class="why__slide"></div> <div class="why__slide"></div> <div class="why__slide"></div> </div> <button class="why__button-left" id="why-left-button">назад</button> <button class="why__button-right" id="why-right-button">вперед</button> There is a JS code
let slides = document.getElementsByClassName('why__slide'); let slideActive = document.querySelector('.why__slide-active'); let index = 0; whyPageRightButton.addEventListener('click', moveRight); function moveRight(p){ if (slides[index] != slides[index].length) { slides[index].classList.remove('why__slide-active'); slides[++index].classList.add('why__slide-active'); } else { slides[0].classList.add('why__slide-active'); } } whyPageLeftButton.addEventListener('click', moveLeft); function moveLeft(){ slides[index].classList.remove('why__slide-active'); slides[index-1].classList.add('why__slide-active'); } now when we click the right button we switch the class why__page-active to the next div. But when we reach the last one, we have to switch back to the first div in the list, I don’t understand what I’m doing wrong, please help with an explanation using the example of this code in pure JS
and also when we press the back button, we switch only to one div back, apparently this happens because index = 0 . Please chew what I am doing wrong, I do not understand the day on the Internet