Skip to main content
0:00est. 5 min

Browser History Bloomberg

// https://leetcode.com/problems/design-browser-history/discuss/938860/JavaScript-clean-solution
/**
* @param {string} homepage
*/
var BrowserHistory = function(homepage) {

this.history = [homepage];
this.currentIdx = 0;
};

/**
* @param {string} url
* @return {void}
*/
BrowserHistory.prototype.visit = function(url) {

this.history.splice(this.currentIdx+1);
this.history.push(url);
this.currentIdx = this.history.length-1;
};

/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.back = function(steps) {
this.currentIdx -= steps;
if(this.currentIdx < 0)
this.currentIdx = 0;
return this.history[this.currentIdx]

};

/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.forward = function(steps) {
this.currentIdx += steps
if(this.currentIdx > this.history.length-1)
this.currentIdx = this.history.length-1
return this.history[this.currentIdx]
};

/**
* Your BrowserHistory object will be instantiated and called as such:
* var obj = new BrowserHistory(homepage)
* obj.visit(url)
* var param_2 = obj.back(steps)
* var param_3 = obj.forward(steps)
*/