Design Browser History LeetCode solution in JavaScript

1
var BrowserHistory = function (homepage) {
2
this.page = {
3
url: homepage,
4
back: null,
5
next: null,
6
};
7
};
8

9
BrowserHistory.prototype.visit = function (url) {
10
this.page.next = {
11
url,
12
back: this.page,
13
next: null,
14
};
15
this.page = this.page.next;
16
};
17

18
BrowserHistory.prototype.back = function (steps) {
19
while (this.page.back && steps) {
20
this.page = this.page.back;
21
steps--;
22
}
23

24
return this.page.url;
25
};
26

27
BrowserHistory.prototype.forward = function (steps) {
28
while (this.page.next && steps) {
29
this.page = this.page.next;
30
steps--;
31
}
32

33
return this.page.url;
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0