Skip to content

Commit 3ffacdf

Browse files
towerOfHanoi added (TheAlgorithms#312)
Co-authored-by: Aayushadh <ayushharwani2011@gmail.com>
1 parent 85dcadc commit 3ffacdf

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Recursive/TowerOfHanoi.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi
2+
// Recursive Javascript function to solve tower of hanoi
3+
4+
function TowerOfHanoi (n, fromRod, toRod, auxRod) {
5+
if (n === 1) {
6+
console.log(`Move disk 1 from rod ${fromRod} to rod ${toRod}`)
7+
return
8+
}
9+
TowerOfHanoi(n - 1, fromRod, auxRod, toRod)
10+
console.log(`Move disk ${n} from rod ${fromRod} to rod ${toRod}`)
11+
TowerOfHanoi(n - 1, auxRod, toRod, fromRod)
12+
}
13+
// Driver code
14+
const n = 4
15+
TowerOfHanoi(n, 'A', 'C', 'B')
16+
// A, C, B are the name of rods

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy