새로운 페이지로 접속할 경우 prev 스택에 원래 있던 페이지를 넣고 next 스택을 비웁니다.
뒤로 가기 버튼을 누를 경우 원래 있던 페이지를 next 스택에 넣고 prev 스택의 top에 있는 페이지로 이동한 뒤 prev 스택의 값을 pop 합니다.
앞으로 가기 버튼을 누를 경우 원래 있던 페이지를 prev 스택에 넣고 next 스택의 top에 있는 페이지로 이동한 뒤 next 스택의 값을 pop 합니다.
브라우저에서 뒤로 가기, 앞으로 가기 버튼이 비활성화일 경우(클릭이 되지 않을 경우)에는 스택에 push 하지 않습니다
인자 1: actions
-> String과 Number 타입을 요소로 갖는 브라우저에서 행동한 순서를 차례대로 나열한 배열
인자 2: start
-> String 타입의 시작 페이지를 나타내는 현재 접속해 있는 대문자 알파벳
출력
-> Array 타입을 리턴해야 합니다.
주의사항
입출력 예시
const actions = ["B", "C", -1, "D", "A", -1, 1, -1, -1];
const start = "A";
const output = browserStack(actions, start);
console.log(output); // [["A"], "B", ["A", "D"]]
const actions2 = ["B", -1, "B", "A", "C", -1, -1, "D", -1, 1, "E", -1, -1, 1];
const start2 = "A";
const output2 = browserStack(actions2, start2);
console.log(output2); // [["A", "B"], "D", ["E"]]
function browserStack(actions, start) {
if(typeof start !== 'string') return false;
let prevStack = [];
let nextStack = [];
let current = start;
for(let i = 0; i < actions.length; i++) {
if(actions[i] === -1 && prevStack.length !== 0) {
let prevPage = prevStack.pop();
nextStack.push(current);
current = prevPage;
} else if(actions[i] === 1 && nextStack.length !== 0) {
let nextPage = nextStack.pop();
prevStack.push(current);
current = nextPage;
} else if(typeof actions[i] === 'string') {
prevStack.push(current);
current = actions[i];
nextStack = [];
}
}
return [prevStack, current, nextStack];
}