Browser Stack Simulation

Source Code

const backStack = [];
const forwardStack = [];
let currentPage = "http://www.acm.org/";
const input = [
    "VISIT http://acm.ashland.edu/",
    "VISIT http://acm.baylor.edu/acmicpc/",
    "BACK",
    "BACK",
    "BACK",
    "FORWARD",
    "VISIT http://www.ibm.com/",
    "BACK",
    "BACK",
    "FORWARD",
    "FORWARD",
    "QUIT"
];
for (const line of input) {
    const parts = line.split(" ");
    const command = parts[0];
    if (command === "QUIT") {
        break;
    } else if (command === "BACK") {
        if (backStack.length > 0) {
            forwardStack.push(currentPage);
            currentPage = backStack.pop();
            console.log(currentPage);
        } else {
            console.log("Ignored");
        }
    } else if (command === "FORWARD") {
        if (forwardStack.length > 0) {
            backStack.push(currentPage);
            currentPage = forwardStack.pop();
            console.log(currentPage);
        } else {
            console.log("Ignored");
        }
    } else if (command === "VISIT") {
        const url = parts[1];
        backStack.push(currentPage);
        currentPage = url;
        forwardStack.length = 0;
        console.log(currentPage);
    }
}
    

Output


    
    
    返回主页