Skip to content

Instantly share code, notes, and snippets.

@jfhector
Last active March 2, 2022 20:50
Show Gist options
  • Save jfhector/885732320934a27d0b14a207dca8a81f to your computer and use it in GitHub Desktop.
Save jfhector/885732320934a27d0b14a207dca8a81f to your computer and use it in GitHub Desktop.
The tower of Hanoi problem solved using recursion
function moveDisks(numberOfDisks, from, to, extra) {
if (numberOfDisks > 0) {
moveDisks(numberOfDisks - 1,
from,
extra,
to
);
console.log(`Moving disk # ${numberOfDisks} from ${from} to ${to}`);
moveDisks(numberOfDisks - 1,
extra,
to,
from
);
}
}
moveDisks(4, "start pole", "target pole", "spare pole");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment