Skip to content

Commit bc1cbed

Browse files
authored
Update 1472-design-browser-history.js
1 parent e387567 commit bc1cbed

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

1472-design-browser-history.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
const BrowserHistory = function(homepage) {
55
this.idx = 0
6+
this.last = 0
67
this.arr = [homepage]
78
};
89

@@ -11,9 +12,9 @@ const BrowserHistory = function(homepage) {
1112
* @return {void}
1213
*/
1314
BrowserHistory.prototype.visit = function(url) {
14-
const n = this.arr.length
15-
this.arr.splice(this.idx + 1, n - this.idx - 1, url)
1615
this.idx++
16+
this.arr[this.idx] = url
17+
this.last = this.idx
1718
};
1819

1920
/**
@@ -37,7 +38,7 @@ BrowserHistory.prototype.back = function(steps) {
3738
* @return {string}
3839
*/
3940
BrowserHistory.prototype.forward = function(steps) {
40-
const n = this.arr.length
41+
const n = this.last + 1
4142
let tmp = this.idx + steps
4243
if(tmp >= n) {
4344
this.idx = n - 1
@@ -47,11 +48,3 @@ BrowserHistory.prototype.forward = function(steps) {
4748
return this.arr[tmp]
4849
}
4950
};
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-
*/

0 commit comments

Comments
 (0)