Last active
February 16, 2016 14:40
-
-
Save chrisrng/8c6ce1531613a208113b to your computer and use it in GitHub Desktop.
Tower of Hanoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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