forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoi.js
More file actions
Latest commit
18 lines (15 loc) · 543 Bytes
/
Copy pathTowerOfHanoi.js
File metadata and controls
18 lines (15 loc) · 543 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// wiki - https://en.wikipedia.org/wiki/Tower_of_Hanoi
// Recursive Javascript function to solve tower of hanoi
exportfunctionTowerOfHanoi(n,from,to,aux,output=[]){
if(n===1){
output.push(`Move disk 1 from rod ${from} to rod ${to}`)
returnoutput
}
TowerOfHanoi(n-1,from,aux,to,output)
output.push(`Move disk ${n} from rod ${from} to rod ${to}`)
TowerOfHanoi(n-1,aux,to,from,output)
returnoutput
}
// Driver code (A, C, B are the name of rods)
// const n = 4
// TowerOfHanoi(n, 'A', 'C', 'B')