Browser History

class BrowserHistory {
  constructor() {
    this.history = [];
    this.index = -1;
  }

  current() {
    if (this.index < 0) {
      return "blank page";
    }
    return this.history[this.index];
  }

  visit(url) {
    this.index += 1;
    this.history[this.index] = url;
    this.history.length = this.index + 1;
  }

  forward() {
    this.index = Math.min(this.index + 1, this.history.length - 1);
  }

  backward() {
    this.index = Math.max(this.index - 1, 0);
  }
}

const bh = new BrowserHistory();
console.log(bh.current());
bh.visit("A");
console.log(bh.current());
bh.visit("B");
console.log(bh.current());
bh.visit("C");
console.log(bh.current());
bh.backward();
console.log(bh.current());
bh.visit("D");
console.log(bh.current());
bh.backward();
console.log(bh.current());
bh.forward();
console.log(bh.current());

// blank page
// A
// B
// C
// B
// D
// B
// D