-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserHistory.js
54 lines (47 loc) · 1.17 KB
/
browserHistory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
/**
* @param {string} homepage
*/
var BrowserHistory = function(homepage) {
this.st = [homepage]
this.otherStack = []
};
/**
* @param {string} url
* @return {void}
*/
BrowserHistory.prototype.visit = function(url) {
this.st.push(url)
this.otherStack = []
};
/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.back = function(steps) {
var minimum = Math.min(this.st.length, steps)
var count = 0
while(minimum!=count){
var poppedElement = this.st.pop()
this.otherStack.push(poppedElement)
count++
}
this.st[this.st.length-1]
};
/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.forward = function(steps) {
var minimum = Math.min(this.otherStack.length, steps)
var count = 0
while(minimum!=count){
var poppedElement = this.otherStack.pop()
this.st.push(poppedElement)
count++
return this.st[this.st.length-1]
}
};
const browserHistory = new BrowserHistory()
browserHistory.visit("asdfasf")