-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path1472-design-browser-history.js
More file actions
56 lines (49 loc) · 1.01 KB
/
1472-design-browser-history.js
File metadata and controls
56 lines (49 loc) · 1.01 KB
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
55
56
/**
* @param {string} homepage
*/
var BrowserHistory = function(homepage) {
this.history = [homepage]
this.curr = 0
};
/**
* @param {string} url
* @return {void}
*/
BrowserHistory.prototype.visit = function(url) {
while(this.history.length>this.curr+1) {
this.history.pop()
}
this.history.push(url)
this.curr++
};
/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.back = function(steps) {
let back = 0
while(back<steps && this.curr>0) {
this.curr--
back++
}
return this.history[this.curr]
};
/**
* @param {number} steps
* @return {string}
*/
BrowserHistory.prototype.forward = function(steps) {
let forward = 0
while(forward<steps && this.curr<this.history.length-1) {
this.curr++
forward++
}
return this.history[this.curr]
};
/**
* 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)
*/