curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68d30fc57588d97fd3027b30.md
On October 28, 1994, Netscape Navigator was released, helping millions explore the early web.
Given an array of browser commands you executed on Netscape Navigator, return the current page you are on after executing all the commands using the following rules:
"Home" page, which will not be included in the commands array."Visit Page": Where "Page" is the name of the page you are visiting. For example, "Visit About" takes you to the "About" page. When you visit a new page, make sure to discard any forward history you have."Back": Takes you to the previous page in your history or stays on the current page if there isn't one."Forward": Takes you forward in the history to the page you came from or stays on the current page if there isn't one.For example, given ["Visit About Us", "Back", "Forward"], return "About Us".
navigate(["Visit About Us", "Back", "Forward"]) should return "About Us".
assert.equal(navigate(["Visit About Us", "Back", "Forward"]), "About Us");
navigate(["Forward"]) should return "Home".
assert.equal(navigate(["Forward"]), "Home");
navigate(["Back"]) should return "Home".
assert.equal(navigate(["Back"]), "Home");
navigate(["Visit About Us", "Visit Gallery"]) should return "Gallery".
assert.equal(navigate(["Visit About Us", "Visit Gallery"]), "Gallery");
navigate(["Visit About Us", "Visit Gallery", "Back", "Back"]) should return "Home".
assert.equal(navigate(["Visit About Us", "Visit Gallery", "Back", "Back"]), "Home");
navigate(["Visit About", "Visit Gallery", "Back", "Visit Contact", "Forward"]) should return "Contact".
assert.equal(navigate(["Visit About", "Visit Gallery", "Back", "Visit Contact", "Forward"]), "Contact");
navigate(["Visit About Us", "Visit Visit Us", "Forward", "Visit Contact Us", "Back"]) should return "Visit Us".
assert.equal(navigate(["Visit About Us", "Visit Visit Us", "Forward", "Visit Contact Us", "Back"]), "Visit Us");
function navigate(commands) {
return commands;
}
function navigate(commands) {
const history = ["Home"];
let currentPageIndex = 0;
for (const command of commands) {
if (command.startsWith("Visit")) {
history.splice(currentPageIndex + 1);
history.push(command.slice(6));
currentPageIndex++;
} else if(command === "Back" && currentPageIndex > 0) {
currentPageIndex--;
} else if (command === "Forward" && currentPageIndex < history.length - 1) {
currentPageIndex++;
}
}
return history[currentPageIndex];
}