Skip to content

Instantly share code, notes, and snippets.

@chrisrng
Last active Feb 16, 2016
Embed
What would you like to do?
Tower of Hanoi
(function(n) {
// DEFAULT to 5 if n is undefined
if (!n) {
console.log("DEFAULT to 5 if n is undefined");
n = 5;
}
var A = Array.apply(0, Array(n)).map(function(x, y){return y+1}).reverse();
var B = [];
var C = [];
var print = function() {
console.log("=====");
console.log(A);
console.log(B);
console.log(C);
}
var func = function(n, from, to, aux) {
if (n > 0) {
func(n-1, from, aux, to);
to.push(from.pop());
print();
func(n-1, aux, to, from);
}
}
print();
func(n, A, C, B);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment