재미있었던 장난감(코플릿)
function browserStack(actions, start) {
if (typeof start !== 'string')
return false;
let nextStack = [];
let prevStack = [];
let now = start;
for ( let i = 0 ; actions.length >= i ; i++ ){
if (actions[i] === -1 && prevStack.length !== 0){
let prevPage = prevStack.pop()
nextStack.push(now)
now = prevPage
}else if (actions[i] === 1 && prevStack.length !== 0){
let nextPage = nextStack.pop()
prevStack.push(now)
now = nextPage
}else if (typeof actions[i] === 'string'){
prevStack.push(now)
now = actions[i]
nextStack = []
}
}
return [prevStack, now, nextStack];
}
1) now를 지역변수로 선언해야하는 이유는?
2) prevPage와 nextPage를 따로 선언해야하는 이유는?
function browserStack(actions, start) {
const present = [];
const next = [];
const prev = [];
if (typeof start !== 'string'){
return false;
}
present[0] = start;
for (let i = 0; i < actions.length; i++){
if(typeof actions[i] == 'string'){
prev.push(present[0]);
present.pop();
present.push(actions[i]);
next.length = 0
}
if(actions[i] === -1 && prev.length != 0){
next.push(present[0]);
present.pop();
present[0] = prev.pop();
}
if(actions[i] === 1 && next.length != 0){
prev.push(present[0]);
present.pop();
present[0] = next.pop();
}
}const result = present[0];
return [prev, result, next]
}
코드를 입력하세요
실력있는 페어분이 캐리하신 코플릿.
주말동안 파봐야겠다.
...어드벤스드도!