Good evening! Here's our prompt for today.
Every modern browser has implemented a logic for keeping track of the users' browsing history. Usually, it keeps track of the websites visited, and their order, so the user can be able to go back and forth between their recently visited websites.
Can you implement a simplified version of a browser history tracker, that will keep track of the order of visited pages (URLs), and will implement logic for visiting the previous, current and following websites?
It should work like this:
We have an empty browser history
[]
Then we visit three websites in the following order: First we visit website A
, then B
, and then C
. Then our browsing history would look like this, having website C
as the page we are currently visiting:
1[ A, B, C]
2 ↑
If we "go back", or call the method back()
, our current website would be B
, our previous would be A
, and the next one will be C
.
1[ A, B, C]
2 ↑
Thus, by "going forward", or calling the method forward()
, we would go to the website C
again.

Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
console.log(browserHistory.forward()); // Should print "https://twitter.com"
// BrowserHistory class definition
class BrowserHistory {
constructor(url) {
this.history = [];
this.pointer = -1;
// fill in
}
visit(url) {
// fill in
}
back() {
// fill in
}
forward() {
// fill in
}
}
// Driver code to test the BrowserHistory class
const browserHistory = new BrowserHistory("https://algodaily.com");
// Visiting new URLs
browserHistory.visit("https://google.com");
browserHistory.visit("https://facebook.com");
// Navigating back and forth
Here's how we would solve this problem...
How do I use this guide?
Browser History Tracker: A Simplified Version
Understanding how browser history works can be intriguing. Browsers are quite efficient at keeping a tab of our actions. Today, we'll create a simplified model of a browser history tracker. This tracker will maintain the sequence of visited pages (URLs) and allow us to navigate forward and backward in history. 🌐
Class Structure and Initial Setup
Let's begin by laying down the groundwork for our Browser History class. We'll need two main properties:
- History Array: To keep track of all the visited URLs.
- Pointer: An integer that points to the current URL we're viewing in the history array.
Here's the initial class setup with a constructor:
1class BrowserHistory {
2 constructor(url) {
3 this.history = [];
4 this.pointer = -1;
5 if (url !== undefined) {
6 this.history.push(url);
7 }
8 }
9}
What's happening here?
- The constructor initializes an empty
history
array and sets thepointer
to -1. - If a URL is provided during the instantiation, it gets added to the
history
.
Putting It All Together
Here's the full BrowserHistory
class with all its glory:
1class BrowserHistory {
2 constructor(url) {
3 this.history = [];
4 this.pointer = 0;
5 if (url !== undefined) {
6 this.history.push(url);
7 }
8 }
9
10 visit(url) {
11 this.history.length = this.pointer + 1;
12 this.history.push(url);
13 this.pointer++;
14 }
15
16 back() {
17 this.pointer = Math.max(0, --this.pointer);
18 }
19
20 forward() {
21 this.pointer = Math.min(this.history.length - 1, ++this.pointer);
22 }
23}
And there you have it—a simplified browser history tracker! This class now allows you to maintain a list of visited URLs and navigate through them just like a real browser.
One Pager Cheat Sheet
- You can create a simplified version of a browser history tracker that will keep track of the order of visited pages (URLs) and implement logic for visiting the previous, current and following websites.
- The
BrowserHistory
class should include ahistory
array, apointer
property and methods for visiting, going back, and going forward to the previously and next visited website URLs.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx
console.log(browserHistory.forward()); // Should print "https://twitter.com"
// BrowserHistory class definition
class BrowserHistory {
constructor(url) {
this.history = [];
this.pointer = -1;
if (url !== undefined) {
this.history.push(url);
this.pointer++;
}
}
visit(url) {
this.history = this.history.slice(0, this.pointer + 1);
this.history.push(url);
this.pointer++;
}
back() {
this.pointer = Math.max(0, --this.pointer);
return this.history[this.pointer];
}
forward() {
this.pointer = Math.min(this.history.length - 1, ++this.pointer);
return this.history[this.pointer];
}
}
// Driver code to test the BrowserHistory class
const browserHistory = new BrowserHistory("https://algodaily.com");
// Visiting new URLs
You're doing a wonderful job. Keep going!
If you had any problems with this tutorial, check out the main forum thread here.