혹시 prev버튼 next버튼있다고 하면 prev버튼을 눌렀을떄 i--, next버튼을 눌렀을떄 i++ 가 되도록하여 URL을 변경할 수 있나요?

조회수 850회
var prevBtn = document.getElementById("prev");
var nextBtn = document.getElementById("next");
var i = 0;
var getPathname = window.location.pathname;
var repetitivePath = getPathname.substring(0, getPathname.lastIndexOf("/") + 1);

function minus(){
    i--;
    if ( i === -1){
        i = 0;
    }
    // var repetitivePath = getPathname.substring(0, getPathname.lastIndexOf("/") + 1);
    // window.location.href = repetitivePath + i + ".html";
    window.location.replace(repetitivePath + i + '.html');
    console.log(i);
    // console.log(repetitivePath);
}

function plus(){
    i++;
    if ( i === 3){
        i = 2;
    }
    // var repetitivePath = getPathname.substring(0, getPathname.lastIndexOf("/") + 1);
    // window.location.href = repetitivePath + i + ".html";
    window.location.replace(repetitivePath + i + '.html');
    console.log(i);
    // console.log(repetitivePath);
}


prevBtn.addEventListener("click", function(){
    minus();
})

nextBtn.addEventListener("click", function(){
    plus()
})

총 파일은 이 코드가 있는 index.html과 바뀔 페이지 파일들인 1.html, 2.html, 3.html로 구성. prev버튼을 눌렀을때 -> i값이 -1되면서 현재 주소의 인덱스값+1 로 하여 i의 값과 ".html" 문자열을 합하여 주소를 변경. next버튼을 눌렀을때 -> i값이 +1되면서 나머지 동일.
처음에 next버튼을 눌렸을때 1.html로는 이동이 됩니다만 2.html로는 더이상 진행이 안됩니다.

어떤 식으로 접근하면 이 문제를 해결 할 수 있을까요? 선배님의 지적 부탁드립니다!

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)