Skip to content

Commit 5144b36

Browse files
Merge pull request akshitagit#139 from manojrayar/master
Create TowerOfHanoi.js
2 parents ace2863 + e507d1d commit 5144b36

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)