Skip to content

Instantly share code, notes, and snippets.

@Colter-Hammer
Created October 18, 2019 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Colter-Hammer/6b9ff7b36cf4400e278b2dffdfd3951d to your computer and use it in GitHub Desktop.
Save Colter-Hammer/6b9ff7b36cf4400e278b2dffdfd3951d to your computer and use it in GitHub Desktop.
A little bit of recursion practice using the game Tower of Hanoi
function move(ring, start, end) {
console.log(`Move ring ${ring} from tower ${start} to tower ${end}`)
}
/*
* hanoi()
* @param - n = Number of rings in game
* @param - start = starting tower
* @param - end = ending tower
* @param - aux = in-between tower
*/
var globalCount = 0
function hanoi(n, start, end, aux) {
let count = 1;
globalCount++;
console.log(globalCount);
if (n === 1) {
move(n, start, end);
return count;
}
count += hanoi(n-1, start, aux, end);
move(n, start, end);
count += hanoi(n-1, aux, end, start);
return count;
}
console.log('Hanoi: ', hanoi(7, 'A', 'B', 'C'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment