Skip to content

Instantly share code, notes, and snippets.

@chrisrng
Last active February 16, 2016 14:40
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 chrisrng/8c6ce1531613a208113b to your computer and use it in GitHub Desktop.
Save chrisrng/8c6ce1531613a208113b to your computer and use it in GitHub Desktop.
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