Back to Freecodecamp

Challenge 236: Browser History

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69b1028d6e265413d0198a2f.md

latest3.4 KB
Original Source

--description--

Given an array of browser commands, return an array with two values: the history as an array of URLs, and the index of the current page.

Valid commands are:

  • "URL" - Where URL is a web address ("freecodecamp.org" for example). Navigates to the given URL, adds it to the history at the next position, and discards any forward history.
  • "Back" - moves to the previous page in history, or stays on the current page if there isn't one.
  • "Forward" - moves to the next page in history, or stays on the current page if there isn't one.

For example, given ["freecodecamp.org", "freecodecamp.org/learn", "Back"], return [["freecodecamp.org", "freecodecamp.org/learn"], 0].

--hints--

getBrowserHistory(["freecodecamp.org", "freecodecamp.org/learn", "Back"]) should return [["freecodecamp.org", "freecodecamp.org/learn"], 0].

js
assert.deepEqual(getBrowserHistory(["freecodecamp.org", "freecodecamp.org/learn", "Back"]), [["freecodecamp.org", "freecodecamp.org/learn"], 0]);

getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]) should return [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3].

js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]), [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3]);

getBrowserHistory(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]) should return [["example.com", "example.com/contact", "example.com/blog"], 1].

js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]), [["example.com", "example.com/contact", "example.com/blog"], 1]);

getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]) should return [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3].

js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]), [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3]);

getBrowserHistory(["example.com", "example.com/about", "Back", "Back"]) should return [["example.com", "example.com/about"], 0].

js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "Back", "Back"]), [["example.com", "example.com/about"], 0]);

getBrowserHistory(["example.com", "example.com/about", "Forward"]) should return [["example.com", "example.com/about"], 1].

js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "Forward"]), [["example.com", "example.com/about"], 1]);

--seed--

--seed-contents--

js
function getBrowserHistory(commands) {

  return commands;
}

--solutions--

js
function getBrowserHistory(commands) {
  const stack = [];
  let index = -1;

  for (const command of commands) {
    if (command === "Back") {
      if (index > 0) index--;
    } else if (command === "Forward") {
      if (index < stack.length - 1) index++;
    } else {
      stack.splice(index + 1);
      stack.push(command);
      index++;
    }
  }

  return [stack, index];
}