---
id: 5951ed8945deab770972ae56
title: Towers of Hanoi
challengeType: 1
forumTopicId: 302341
dashedName: towers-of-hanoi
---

# --description--

Solve the Towers of Hanoi problem. The number of objects will be given as the first parameter, followed by the strings used to identify each stack of objects. Create a nested array containing the list of moves, `["source", "destination"]`.
 
For example, the parameters `(4, 'A', 'B', 'C')`, will result in nested array of moves `[['A', 'C'], ['B', 'A']]`, indicating that the 1st move was to move an object from stack `A` to `C` and the 2nd move was to move an object from stack `B` to `A`.

# --instructions--

Write a function that returns the moves to stack the objects in a nested array.

# --before-each--

```js
const res3Moves = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['A', 'B'], ['C', 'A'], ['C', 'B'], ['A', 'B']];
const res7First10Moves = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['A', 'B'], ['C', 'A'], ['C', 'B'], ['A', 'B'], ['A', 'C'], ['B', 'C'], ['B', 'A']];

function getTowerResults_() {
  return {
    res3: towerOfHanoi(3, 'A', 'B', 'C'),
    res5: towerOfHanoi(5, 'X', 'Y', 'Z'),
    res7: towerOfHanoi(7, 'A', 'B', 'C')
  };
}
```

# --hints--

`towerOfHanoi` should be a function.

```js
assert(typeof towerOfHanoi === 'function');
```

`towerOfHanoi(3, ...)` should return 7 moves.

```js
assert(getTowerResults_().res3.length === 7);
```

`towerOfHanoi(3, 'A', 'B', 'C')` should return `[['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B']]`.

```js
assert.deepEqual(getTowerResults_().res3, res3Moves);
```

`towerOfHanoi(5, "X", "Y", "Z")` 10th move should be Y -> X.

```js
const { res5 } = getTowerResults_();
assert.deepEqual(res5[9], ['Y', 'X']);
```

`towerOfHanoi(7, 'A', 'B', 'C')` first ten moves should be `[['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B'], ['A','C'], ['B','C'], ['B','A']]`

```js
const { res7 } = getTowerResults_();
assert.deepEqual(res7.slice(0, 10), res7First10Moves);
```

# --seed--

## --seed-contents--

```js
function towerOfHanoi(n, a, b, c) {

  return [[]];
}
```

# --solutions--

```js
function towerOfHanoi(n, a, b, c) {
  const res = [];
  towerOfHanoiHelper(n, a, c, b, res);
  return res;
}

function towerOfHanoiHelper(n, a, b, c, res) {
  if (n > 0) {
    towerOfHanoiHelper(n - 1, a, c, b, res);
    res.push([a, c]);
    towerOfHanoiHelper(n - 1, b, a, c, res);
  }
}
```
