---
id: 69b1028d6e265413d0198a2f
title: "Challenge 236: Browser History"
challengeType: 29
dashedName: challenge-236
---

# --description--

Given an array of browser commands, return an array with two values: the history as an array of URLs, and the index of the current page.

Valid commands are:

- `"URL"` - Where URL is a web address (`"freecodecamp.org"` for example). Navigates to the given URL, adds it to the history at the next position, and discards any forward history.
- `"Back"` - moves to the previous page in history, or stays on the current page if there isn't one.
- `"Forward"` - moves to the next page in history, or stays on the current page if there isn't one.

For example, given `["freecodecamp.org", "freecodecamp.org/learn", "Back"]`, return `[["freecodecamp.org", "freecodecamp.org/learn"], 0]`.

# --hints--

`get_browser_history(["freecodecamp.org", "freecodecamp.org/learn", "Back"])` should return `[["freecodecamp.org", "freecodecamp.org/learn"], 0]`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["freecodecamp.org", "freecodecamp.org/learn", "Back"]), [["freecodecamp.org", "freecodecamp.org/learn"], 0])`)
}})
```

`get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog"])` should return `[["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3]`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]), [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3])`)
}})
```

`get_browser_history(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"])` should return `[["example.com", "example.com/contact", "example.com/blog"], 1]`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]), [["example.com", "example.com/contact", "example.com/blog"], 1])`)
}})
```

`get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"])` should return `[["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3]`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]), [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3])`)
}})
```

`get_browser_history(["example.com", "example.com/about", "Back", "Back"])` should return `[["example.com", "example.com/about"], 0]`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Back", "Back"]), [["example.com", "example.com/about"], 0])`)
}})
```

`get_browser_history(["example.com", "example.com/about", "Forward"])` should return `[["example.com", "example.com/about"], 1]`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Forward"]), [["example.com", "example.com/about"], 1])`)
}})
```

# --seed--

## --seed-contents--

```py
def get_browser_history(commands):

    return commands
```

# --solutions--

```py
def get_browser_history(commands):
    stack = []
    index = -1

    for command in commands:
        if command == "Back":
            if index > 0:
                index -= 1
        elif command == "Forward":
            if index < len(stack) - 1:
                index += 1
        else:
            del stack[index + 1:]
            stack.append(command)
            index += 1

    return [stack, index]
```
