Skip to content

Commit e507d1d

Browse files
authored
Create TowerOfHanoi.js
1 parent ace2863 commit e507d1d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Sorts/TowerOfHanoi.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Function to solve Tower of Hanoi, with recursion.
2+
function towerOfHanoi(n, from_rod, to_rod, aux_rod)
3+
{
4+
if (n == 0)
5+
{
6+
return;
7+
}
8+
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
9+
document.write("Move disk " + n + " from rod " + from_rod +
10+
" to rod " + to_rod+"<br/>");
11+
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
12+
}
13+
// Driver code
14+
var N = 3;
15+
16+
// A, B and C are names of rods
17+
towerOfHanoi(N, 'A', 'C', 'B');

0 commit comments

Comments
 (0)