Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Tower of Hanoi Moves

A Closer Look at Functionshard

Course · Section 10: A Closer Look at Functions · Lecture 137: First-Class and Higher-Order Functions

Implement `hanoi(n)` returning the minimum number of moves to solve the Tower of Hanoi with `n` disks, using recursion.

Sample tests

Input: hanoi(3)
Output: 7
Input: hanoi(1)
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Trying to simulate the pegs when only the move count is needed.

Learning resources

  • MDN: Recursion
Approach & explanation (try first)

Moving n disks needs moving n-1 twice plus one move, which resolves to 2^n - 1.

Loading...
⌘/Ctrl + Enter

Run your code to see results.