File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } homepage
3
+ */
4
+ const BrowserHistory = function ( homepage ) {
5
+ this . idx = 0
6
+ this . arr = [ homepage ]
7
+ } ;
8
+
9
+ /**
10
+ * @param {string } url
11
+ * @return {void }
12
+ */
13
+ BrowserHistory . prototype . visit = function ( url ) {
14
+ const n = this . arr . length
15
+ this . arr . splice ( this . idx + 1 , n - this . idx - 1 , url )
16
+ this . idx ++
17
+ } ;
18
+
19
+ /**
20
+ * @param {number } steps
21
+ * @return {string }
22
+ */
23
+ BrowserHistory . prototype . back = function ( steps ) {
24
+ const idx = this . idx
25
+ let tmp = idx - steps
26
+ if ( tmp < 0 ) {
27
+ this . idx = 0
28
+ return this . arr [ 0 ]
29
+ } else {
30
+ this . idx = tmp
31
+ return this . arr [ tmp ]
32
+ }
33
+ } ;
34
+
35
+ /**
36
+ * @param {number } steps
37
+ * @return {string }
38
+ */
39
+ BrowserHistory . prototype . forward = function ( steps ) {
40
+ const n = this . arr . length
41
+ let tmp = this . idx + steps
42
+ if ( tmp >= n ) {
43
+ this . idx = n - 1
44
+ return this . arr [ n - 1 ]
45
+ } else {
46
+ this . idx = tmp
47
+ return this . arr [ tmp ]
48
+ }
49
+ } ;
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