File tree Expand file tree Collapse file tree 1 file changed +4
-11
lines changed Expand file tree Collapse file tree 1 file changed +4
-11
lines changed Original file line number Diff line number Diff line change 3
3
*/
4
4
const BrowserHistory = function ( homepage ) {
5
5
this . idx = 0
6
+ this . last = 0
6
7
this . arr = [ homepage ]
7
8
} ;
8
9
@@ -11,9 +12,9 @@ const BrowserHistory = function(homepage) {
11
12
* @return {void }
12
13
*/
13
14
BrowserHistory . prototype . visit = function ( url ) {
14
- const n = this . arr . length
15
- this . arr . splice ( this . idx + 1 , n - this . idx - 1 , url )
16
15
this . idx ++
16
+ this . arr [ this . idx ] = url
17
+ this . last = this . idx
17
18
} ;
18
19
19
20
/**
@@ -37,7 +38,7 @@ BrowserHistory.prototype.back = function(steps) {
37
38
* @return {string }
38
39
*/
39
40
BrowserHistory . prototype . forward = function ( steps ) {
40
- const n = this . arr . length
41
+ const n = this . last + 1
41
42
let tmp = this . idx + steps
42
43
if ( tmp >= n ) {
43
44
this . idx = n - 1
@@ -47,11 +48,3 @@ BrowserHistory.prototype.forward = function(steps) {
47
48
return this . arr [ tmp ]
48
49
}
49
50
} ;
50
-
51
- /**
52
- * Your BrowserHistory object will be instantiated and called as such:
53
- * var obj = new BrowserHistory(homepage)
54
- * obj.visit(url)
55
- * var param_2 = obj.back(steps)
56
- * var param_3 = obj.forward(steps)
57
- */
You can’t perform that action at this time.
0 commit comments